Unnie Ayilliath

Microsoft 365 | Azure | Identity Solutions

SharePoint 2013 – Update/Clear Taxonomy Field Value using C# CSOM

This post will detail you with sample code snippet to update and clear value of a  taxonomy field of a list item.

Update value to Taxonomy Field:

public void UpdateTaxonomyField(ClientContext ctx, List list,ListItem listItem,string fieldName,string fieldValue)
            Field field = list.Fields.GetByInternalNameOrTitle(fieldName);
            TaxonomyField txField = clientContext.CastTo<TaxonomyField>(field);
            TaxonomyFieldValue termValue = new TaxonomyFieldValue();
            string[] term = fieldValue.Split('|');
            termValue.Label = term[0];
            termValue.TermGuid = term[1];
            termValue.WssId = -1;
            txField.SetFieldValueByValue(listItem, termValue);

Here make sure you pass the term to be updated in the format termname|guid.

Note: This is just one of the methods using which you can update the Taxonomy field value. There are other methods such as :

TaxonomyFieldValue.SetFieldValueByTerm :  Here just the parameters are different.

txField.SetFieldValueByTerm(listItem,termObj,lcid); // Here termObj is an object of Term. lcid is the language code identifier (LCID) of the language of the default Label to use. 

Clearing Value of a Taxonomy Field:

Interestingly to set the value of a Taxonomy Field there are a lot of methods available , but none of those methods work if you want to clear the value. The only one which actually works is TaxonomyField.ValidateSetValue method.

public void ClearTaxonomyFieldValue(ClientContext ctx, List list, ListItem listItem, string fieldName)
            Field field = list.Fields.GetByInternalNameOrTitle(fieldName);
            TaxonomyField txField = clientContext.CastTo(field);
            txField.ValidateSetValue(listItem, null);

Hope this post saves your time 🙂

Published by

4 responses to “SharePoint 2013 – Update/Clear Taxonomy Field Value using C# CSOM”

  1. thirupathi jonnala Avatar
    thirupathi jonnala

    Thank you very much..this resolved the issue.

  2. What is fieldValue supposed to look like?

    1. The fieldValue should be in the format “termname|guid” e.g. Australia|f03bad5a-3508-4e86-90a9-4b456ada5b12

  3. Thanks greaat blog post

Leave a Reply to Unnikannan Ayilliath Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Website Powered by WordPress.com.

%d bloggers like this: