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

Sathish Nadarajan
 
Solution Architect
February 11, 2018
 
Rate this article
[Total: 0    Average: 0/5]
 
Views
5061

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.

Category : CSOM, Office 365, PNP, SharePoint

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
[Total: 0    Average: 0/5]
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 13+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment