SharePoint Update several managed metadata fields with one ExecuteQuery()

Occasional Contributor

I have C# CSOM code that updates a single managed metadata field value, but I found that I have to perform the following after each field is updated:

 

item.Update();

//context.Load(item);
context.ExecuteQuery();

 

otherwise only the last field is set correctly. When I have several managed metadata fields, this seems really inefficient and causes multiple versions of my document to be posted, one for each field update - can I update all my managed metadata fields using a single ExecuteQuery()? This code is used in SP 2013 and SPO.

 

 

 

 

 

            if (taxField.AllowMultipleValues)
            {
                termValues = item[fieldName] as TaxonomyFieldValueCollection;
                foreach (TaxonomyFieldValue tv in termValues)
                {
                    termValueString += tv.WssId + ";#" + tv.Label + "|" + tv.TermGuid + ";#";
                }
                termValueString += "-1;#" + termValueString + "|" + termId;
                termValues = new TaxonomyFieldValueCollection(context, termValueString, taxField);
                taxField.SetFieldValueByValueCollection(item, termValues);
            }
            else
            {
                termValue = new TaxonomyFieldValue();
                termValue.Label = fieldValue;
                termValue.TermGuid = termId;
                termValue.WssId = -1;
                taxField.SetFieldValueByValue(item, termValue);
            }
            item.Update();
            //context.Load(item);
            context.ExecuteQuery();

 

 

 

 

single 

0 Replies