1

I am trying to copy a Listitem having Taxonomy column with multi values from one list to another list in a sharepoint online. In the both lists Taxonomy metadata column are created. I am able to get the TaxonomyFieldValueCollection values but some how i am not able to set the values in the destination lists. I have gone through the stackeoverflow post link, but unable to set the Taxonomy values in the Destination list. Below is my code.Iam getting no error

    using (var sourceContext = new ClientContext(SourceSiteCollectionurl))
            {
                sourceContext.Credentials = new SharePointOnlineCredentials(UserName, secure);
                List SourceList = sourceContext.Web.Lists.GetByTitle(listName);
                Microsoft.SharePoint.Client.ListItem item = SourceList.GetItemById(1);
                sourceContext.Load(item);
                sourceContext.ExecuteQuery();
                TaxonomyFieldValueCollection sourceValue = item["Related_x0020_Party"] as TaxonomyFieldValueCollection;
                string ss = "";  
                 List<string> termValues = new List<string>();
                foreach (TaxonomyFieldValue taxProductFieldValue in sourceValue)
                {
                    // sb_ProductFieldValue.Append(taxProductFieldValue.TermGuid + "|");
                     termValues.Add(taxProductFieldValue.WssId + ";#" + taxProductFieldValue.Label + "|" + taxProductFieldValue.TermGuid);
                  //  ss += taxProductFieldValue.Label + "|" + taxProductFieldValue.TermGuid + ";";
                }
               List targetList = sourceContext.Web.Lists.GetByTitle("Project Department");
                Microsoft.SharePoint.Client.ListItem targetItem = targetList.GetItemById(2);
                sourceContext.Load(targetList.Fields);
                sourceContext.Load(targetItem);
                sourceContext.ExecuteQuery();
                targetItem["Related_x0020_Party"] = termValues;
                targetItem.Update();
                sourceContext.Load(targetItem);
                sourceContext.ExecuteQuery();
    }

Any help Please..

Update: I tried to set the values using List termValues = new List(); but the filed is not updating. No error no output

2 Answers 2

4

Finally the below code works fine for me. Creating a list of string type and joining the array items with ";"

        using (var sourceContext = new ClientContext(SourceSiteCollectionurl))
            {
                sourceContext.Credentials = new SharePointOnlineCredentials(UserName, secure);
                List SourceList = sourceContext.Web.Lists.GetByTitle(listName);
                Microsoft.SharePoint.Client.ListItem item = SourceList.GetItemById(1);
                sourceContext.Load(item);
                sourceContext.ExecuteQuery();
                TaxonomyFieldValueCollection sourceValue = item["Related_x0020_Party"] 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 strtermValues = string.Join(";", termValuesarrary);
                List targetList = sourceContext.Web.Lists.GetByTitle("Project Department");
                Microsoft.SharePoint.Client.ListItem targetItem = targetList.GetItemById(2);
                sourceContext.Load(targetList.Fields);
                sourceContext.Load(targetItem);
                sourceContext.ExecuteQuery();
                targetItem["Related_x0020_Party"] = strtermValues;
                targetItem.Update();
                sourceContext.Load(targetItem);
                sourceContext.ExecuteQuery();
            }
2

Did you try the following:

targetItem["Related_x0020_Party"] = sourceValue;
targetItem.Update();
sourceContext.Load(targetItem);
sourceContext.ExecuteQuery();
3
  • I tried and getting the error "The object is not associated with an object identity or the object identity is invalid."
    – TARUN
    Mar 15, 2017 at 6:28
  • This error appears on which line targetItem.Update(); or sourceContext.ExecuteQuery(); Mar 15, 2017 at 6:39
  • targetItem["Related_x0020_Party"] = sourceValue; At this line I am getting the error
    – TARUN
    Mar 15, 2017 at 6:44

Your Answer

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

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