How to Update the Taxonomy Field (Single Value or Multiple Value) using C# Patterns and Practices in SharePoint Office 365


Sathish Nadarajan
SharePoint MVP
Published On :   11 Feb 2018
Visit Count
Today :  10    Total :   606
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

SharePoint Office 365 Tool
Simple & Powerful Tool for Migration, Security & Reporting. Free Trial


In the earlier article, we saw how to create the terms in the Termstore and subsequently, now, we are going to update the List Item with the new terms created.

The below piece of code will update the Single Value Managed Metadata Column with the newly created Terms. The EnsureTerms method, I request the readers to go to the earlier article.

 public static void AddTaxonomyEntry()
         {
             try
             {
                 OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
                 string siteUrl = "https://*****.sharepoint.com/sites/DeveloperSite";
                 string userName = "Sathish@*********.onmicrosoft.com";
                 string password = "******";
 
                 using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
                 {
                     Web web = ctx.Web;
                     ctx.Load(web);
                     ctx.Load(web.Lists);
                     ctx.ExecuteQueryRetry();
                     List list = web.Lists.GetByTitle("List1");
                     ctx.Load(list);
                     ctx.ExecuteQueryRetry();
 
                     var tags = new string[] { "Term2"};
 
                     var tagsString = EnsureTerms(tags, siteUrl, list.Id, "TaxonomyField",ctx);
 
 
                     ListItem listItem = list.AddItem(new ListItemCreationInformation());
                     listItem["Title"] = "Test Title";
                     listItem.Update();
 
                     ctx.Load(listItem);
                     ctx.ExecuteQuery();
 
                     var clientRuntimeContext = listItem.Context;
                     
                     var field = list.Fields.GetByInternalNameOrTitle("TaxonomyField");
                     var taxKeywordField = clientRuntimeContext.CastTo<TaxonomyField>(field);
 
                     TaxonomyFieldValue termValue = new TaxonomyFieldValue();
                     string[] term = tagsString.Split('|');
                     termValue.Label = term[0];
                     termValue.TermGuid = term[1];
                     termValue.WssId = -1;
                     taxKeywordField.SetFieldValueByValue(listItem, termValue);
 
                     taxKeywordField.Update();
 
                     listItem.Update();
                     ctx.Load(listItem);
                     ctx.ExecuteQuery();
                 }
             }
             catch (Exception ex) { }
         }
 
 
This will update the Taxonomy Field.

To update the Multi Value Taxonomy Field, the below lines needs to be replaced.

 //TaxonomyFieldValue termValue = new TaxonomyFieldValue();
                     //string[] term = tagsString.Split('|');
                     //termValue.Label = term[0];
                     //termValue.TermGuid = term[1];
                     //termValue.WssId = -1;
                     //taxKeywordField.SetFieldValueByValue(listItem, termValue);
 
                     taxKeywordField.SetFieldValueByValueCollection(listItem, new TaxonomyFieldValueCollection(clientRuntimeContext, tagsString, taxKeywordField));
                     taxKeywordField.Update();
 

Happy Coding,

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

KWizCom Forms App