Updating Term Store Field Value

There appears to be various different recommendations of ways for updating a term store field value and I’d like to chip in with mine.

Some recommend updating by setting the list item’s hidden taxonomy field, which looks something like the following.

First, get the Guid for the term we need to update:

// The value we want to change to
CustomEnumValues customEnumValue = CustomEnumValues.CustomValue;
// Get SharePoint taxonomy field object that we want to update to value it controls
TaxonomyField taxField = listItem.Fields[fieldGuid] as TaxonomyField;
// Get the taxonomy session for the current site
TaxonomySession taxSession = new TaxonomySession(SPContext.Current.Site);
// Get the default term store object for this site.
TermStore taxTermStore = taxSession.DefaultSiteCollectionTermStore;
TermSet termSet = taxTermStore.GetTermSet(taxField.TermSetId);
Guid termGuid = Guid.Empty;
string enumValueName = Enum.GetName(typeof(CustomEnumValues), customEnumValue);
foreach (Term term in termSet.Terms)
    // Check if the term matches our enum value
    if (term.Name.Equals(enumValueName, StringComparison.OrdinalIgnoreCase))
        termGuid = term.Id;

Now that we have the term Guid, we attempt to update the list item’s field value:

if (termGuid != Guid.Empty)
    Term customTerm = termSet.GetTerm(termGuid);
    string taxFieldInternalName = listItem.Fields[taxField.TextField].InternalName;
    listItem[taxFieldInternalName] = customTerm.Name + "|" + customTerm.Id.ToString();
    // Update the list item

Testing this, it appears to work most of the time, but I got varied results where the term field value did not always reflect the change made. For instance, after running code similar to the above, the field value did not update. Looking at the XML for the document I found the correct value had been set against the hidden field ows_NameOfTaxonomyFieldTaxHTFField0 but the previous value was still set for the display field ows_NameOfTaxonomyField.

By also updating the actual taxonomy field with the new value I managed to get the update to consistently work.

The following is an update to the second section of code above.

if (termGuid != Guid.Empty)
    Term customTerm = termSet.GetTerm(termGuid);
    // Update the internal taxonomy string field value with label Guid pair
    string taxFieldInternalName = listItem.Fields[taxField.TextField].InternalName;
    listItem[taxFieldInternalName] = customTerm.Name + "|" + customTerm.Id.ToString();
    // Also update the field itself using the static SetFieldValue method of the taxonomy field object.
    taxField.SetFieldValue(listItem, customTerm);
    // Update the list item with the new taxonomy field value

In the above code we call the taxonomy field object’s SetFieldValue method and pass the list item and new taxonomy term object. For whatever reason, possibly environmental, just calling this and not setting the internal field value does not always update the item in the UI. I inspected the XML for the list item after calling the update method and the internal taxonomy field’s value had not been updated after only calling the SetFieldValue method. So to be completely safe, I decided to also explicitly update the internal field value.

There is an alternate way of updating the term value and this is by retrieving the field value as a TaxonomyFieldValue object and call it’s PopulateFromLabelGuidPair method, but again, this does not appear to consistently update the taxonomy field.

Obviously the code requires error handling, but hopefully it will give someone a base to work with.

This entry was posted in SharePoint, Taxonomy and tagged , . Bookmark the permalink.

6 Responses to Updating Term Store Field Value

  1. Ismail says:

    Thank you so much, this is really helpful.

  2. Emiliano says:

    Hi Stuart! Nice Post, really helpfull.
    I’m Emiliano from Argentina and I got an issue with the navigation hierachy.
    When I try to filter documents by the navigation hierachy, a few terms doesn’t return documents (Previously I run a program that set metadata values, and my code it’s almost the same as yours). But if I search this document by the Key Filter there’s no problem, even the “bugged” Term start to work fine.
    Also if a create a view that searchs the docuemnt, the “bugged” Term start to work fine. It seems like a cache problem, but I’m really not sure.
    Do have any idea of why this happens?

    Sorry for my English xD
    And Thanks!

    • Stuart says:

      Hi Emiliano,

      I’ve found the term store to be unstable, especially when working on it in a development environment with caching. Making changes to a solution and deploying\upgrading it sometimes causes some of the terms to stop working and the only solution appears to be to delete the term group and re-create the site – not a solution for a live production environment by any means!

      That doesn’t explain why the Key Filter works for all your terms while the navigation hierarchies only for some.

      It sounds like a caching issue, but without seeing the environment and configuration I’d be guessing at the cause.

      It might be worth having a look in the hidden list called “TaxonomyHiddenList” located in the root web of your site collection. This is a list that SharePoint uses to maintain the keywords and terms for the site collection.

  3. Johnetta says:

    If my prbeolm was a Death Star, this article is a photon torpedo.

  4. Clyde Davies says:

    Thank you X 10^6. I have been battling for a couple of days to try to get this to work and getting nowhere, and this post has saved my sanity.

  5. Jeffrey says:

    Thanks your blog is the only taxonomy that is actually working consistently in Sharepoint.


Leave a Reply

Your email address will not be published. Required fields are marked *

Solve the maths problem shown below before posting: *