2

I am trying to Copy a Document having the Managed Metadata(Termsets) fields from one site collection to another site collection in SharePoint online using CSOM. The Managed Metadata Fields are there in both the Document libraries with different Internal names but with same display name. The Columns names are shown in below image. After Copying the document only Topic filed data is copied without any additional coding.

Taxonomyfiledar

So I tried the below code to copy Managed Metadata(Termsets) fields after Uploading the file with internals names but the fields are not updating.

foreach (var stritem in Taxonomyfiledarray)
    {
    TaxonomyFieldValueCollection sourceValue = SourcelistItem[stritem.Source] as TaxonomyFieldValueCollection; 
    string[] termValuesarrary;
    List<string> termValues = new List<string>();
    foreach (TaxonomyFieldValue taxProductFieldValue in sourceValue)
    {
     termValues.Add(taxProductFieldValue.Label + "|" + taxProductFieldValue.TermGuid);
    }
    termValuesarrary = termValues.ToArray();
    string termValuesstring = string.Join(";", termValuesarrary);
    itemToCreate[stritem.Destination] = termValuesstring;
    }
    itemToCreate.Update();

Any Suggestion please..

2 Answers 2

1

With assumption that both of the field use same TermSet, ExecuteQuery() is also called after this, try modified code:

foreach (var stritem in Taxonomyfiledarray)
{
TaxonomyFieldValueCollection sourceValue = SourcelistItem[stritem.Source] as TaxonomyFieldValueCollection; 
string[] termValuesarrary;
List<string> termValues = new List<string>();
foreach (TaxonomyFieldValue taxProductFieldValue in sourceValue)
{
 termValues.Add("-1;#" + taxProductFieldValue.Label + "|" + taxProductFieldValue.TermGuid);
}
termValuesarrary = termValues.ToArray();
string termValuesstring = string.Join(";#", termValuesarrary);
itemToCreate[stritem.Destination] = termValuesstring;
}
itemToCreate.Update();
8
  • I tried the code but no change in the output .. And both of the fields use same TermSet
    – TARUN
    Commented Mar 27, 2017 at 7:20
  • Have changes been made as per code above in line 8 & line 11. Please provide a sample value that you are trying to save. Also, pls confirm if both columns support multi selections? Commented Mar 27, 2017 at 7:32
  • Yes i changed the code(added + sign after "-1;#" ) and tested the code..
    – TARUN
    Commented Mar 27, 2017 at 7:32
  • in join function as well, has it been changed to ";#" instead of ";" Commented Mar 27, 2017 at 7:41
  • Many thanks for your effort. Tested with termValues.Add ("-1;#" + taxProductFieldValue.Label + "|" + taxProductFieldValue.TermGuid); and string termValuesstring = string.Join(";#", termValuesarrary);. The out put has no effect only Topic field was copying
    – TARUN
    Commented Mar 27, 2017 at 7:44
0

@Alok Kumar Gupta: Thanks for you guidance. Your guidance and the linkSetting a Managed Metadata Field with the Client Object Model driven me to fix the issue.

                 foreach (var stritem in Taxonomyfiledarray)
                    {
                        try
                        {

                            TaxonomyFieldValueCollection newtermValues = null;
                            Field field = fields.GetByInternalNameOrTitle(stritem.Destination);
                            destinationContext.Load(field);
                            destinationContext.ExecuteQuery();
                            TaxonomyField txField = destinationContext.CastTo<TaxonomyField>(field);
                            if (txField.AllowMultipleValues)
                            {
                                TaxonomyFieldValueCollection sourceValue = SourcelistItem[stritem.Source] as TaxonomyFieldValueCollection;
                                string[] termValuesarrary;
                                List<string> termValues = new List<string>();
                                foreach (TaxonomyFieldValue taxProductFieldValue in sourceValue)
                                {
                                    // termValues.Add(taxProductFieldValue.Label + "|" + taxProductFieldValue.TermGuid);
                                    termValues.Add("-1;#" + taxProductFieldValue.Label + "|" + taxProductFieldValue.TermGuid);
                                }
                                termValuesarrary = termValues.ToArray();
                                string termValuesstring = string.Join(";#", termValuesarrary);
                                newtermValues = new TaxonomyFieldValueCollection(destinationContext, termValuesstring, txField);
                                txField.SetFieldValueByValueCollection(itemToCreate, newtermValues);
                                itemToCreate.Update();
                                destinationContext.Load(itemToCreate);
                                destinationContext.ExecuteQuery();
                            }

                        }
                        catch (Exception ex)
                        {
                            lbl_Error.Text = ex.Message.ToString();
                        }
                    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.