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;
        break;
    }
}

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
    listItem.SystemUpdate(false);
}

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
    listItem.SystemUpdate(false);
}

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.
0 0 votes
Article Rating
Subscribe
Notify of
guest

Solve the maths problem shown below before posting: *

6 Comments
Inline Feedbacks
View all comments
Ismail

Thank you so much, this is really helpful.

Emiliano

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!

Johnetta

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

Clyde Davies

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.

Jeffrey

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

🙂