Update a managed metadata column with multiple values using CSOM

1

I have the following code inside my remote event reciever, to create a new folder, and then populate the folder managed metadata fields with the current item (item that was added) values:-

ListItem listItem = context.Web.GetList(webrelativeurl +  "/"+i2["TemplateListUrl"]).AddItem(listItemCreationInformation);

listItem["FolderType"] = currentitem["FolderType"];
listItem["CorpType"] = currentitem["CorpType"];

now the FodlerType on both the current item and the listitem is a managed metadata column which allow single value only, while the CorpType allow multiple values. my above code will assign a value for the FolderType correctly, but will raise this error when updating the CorpType:-

Microsoft.SharePoint.Client.ClientRequestException
HResult=0x80131500 Message=The object is not associated with an object identity or the object identity is invalid. Source= StackTrace:

any advice?

c#
sharepoint
sharepoint-online
office365-apps
asked on Stack Overflow Feb 17, 2020 by test test

1 Answer

0

You need to get a TaxonomyFieldValueCollection use the SetFieldValueByValueCollection of the TaxonomyField object.

var field = item.ParentList.Fields.GetByInternalNameOrTitle("CorpType");
var corpTypeTaxField = context.CastTo<TaxonomyField>(field);
context.Load(item,i=>i[TaxKeywordFieldName]);
var valuesToCopy = corpTypeTaxField.GetFieldValueByValueCollection(currentitem["CorpType"].ToString());
corpTypeTaxField.SetFieldValueByValueCollection(listItem, valuesToCopy);    

Note, this gets a lot messier if you have terms that you need to look up to construct a TaxonomyFieldValueCollection from scratch -- copying it from another item is definitely easier.

answered on Stack Overflow Feb 17, 2020 by willman

User contributions licensed under CC BY-SA 3.0