{"id":135,"date":"2010-11-19T22:22:35","date_gmt":"2010-11-19T21:22:35","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=135"},"modified":"2010-11-25T12:24:49","modified_gmt":"2010-11-25T12:24:49","slug":"updating-term-store-field-value","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2010\/11\/19\/updating-term-store-field-value\/","title":{"rendered":"Updating Term Store Field Value"},"content":{"rendered":"<p>There appears to be various different recommendations of ways for updating a term store field value and I&#8217;d like to chip in with mine.<\/p>\n<p>Some recommend updating by setting the list item&#8217;s hidden taxonomy field, which looks something like the following.<\/p>\n<p>First, get the Guid for the term we need to update:<br \/>\n<!--more--><\/p>\n<pre lang=\"csharp\">\r\n\/\/ The value we want to change to\r\nCustomEnumValues customEnumValue = CustomEnumValues.CustomValue;\r\n\r\n\/\/ Get SharePoint taxonomy field object that we want to update to value it controls\r\nTaxonomyField taxField = listItem.Fields[fieldGuid] as TaxonomyField;\r\n\/\/ Get the taxonomy session for the current site\r\nTaxonomySession taxSession = new TaxonomySession(SPContext.Current.Site);\r\n\/\/ Get the default term store object for this site.\r\nTermStore taxTermStore = taxSession.DefaultSiteCollectionTermStore;\r\nTermSet termSet = taxTermStore.GetTermSet(taxField.TermSetId);\r\n\r\nGuid termGuid = Guid.Empty;\r\nstring enumValueName = Enum.GetName(typeof(CustomEnumValues), customEnumValue);\r\n\r\nforeach (Term term in termSet.Terms)\r\n{\r\n    \/\/ Check if the term matches our enum value\r\n    if (term.Name.Equals(enumValueName, StringComparison.OrdinalIgnoreCase))\r\n    {\r\n        termGuid = term.Id;\r\n        break;\r\n    }\r\n}\r\n<\/pre>\n<p>Now that we have the term Guid, we attempt to update the list item&#8217;s field value:<\/p>\n<pre lang=\"csharp\">\r\nif (termGuid != Guid.Empty)\r\n{\r\n    Term customTerm = termSet.GetTerm(termGuid);\r\n    \r\n    string taxFieldInternalName = listItem.Fields[taxField.TextField].InternalName;\r\n    listItem[taxFieldInternalName] = customTerm.Name + \"|\" + customTerm.Id.ToString();\r\n    \/\/ Update the list item\r\n    listItem.SystemUpdate(false);\r\n}\r\n<\/pre>\n<p>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.<\/p>\n<p>By also updating the actual taxonomy field with the new value I managed to get the update to consistently work.<\/p>\n<p>The following is an update to the second section of code above.<\/p>\n<pre lang=\"csharp\">\r\nif (termGuid != Guid.Empty)\r\n{\r\n    Term customTerm = termSet.GetTerm(termGuid);\r\n    \r\n    \/\/ Update the internal taxonomy string field value with label Guid pair\r\n    string taxFieldInternalName = listItem.Fields[taxField.TextField].InternalName;\r\n    listItem[taxFieldInternalName] = customTerm.Name + \"|\" + customTerm.Id.ToString();\r\n    \r\n    \/\/ Also update the field itself using the static SetFieldValue method of the taxonomy field object.\r\n    taxField.SetFieldValue(listItem, customTerm);\r\n    \r\n    \/\/ Update the list item with the new taxonomy field value\r\n    listItem.SystemUpdate(false);\r\n}\r\n<\/pre>\n<p>In the above code we call the taxonomy field object&#8217;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&#8217;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.<\/p>\n<p>There is an alternate way of updating the term value and this is by retrieving the field value as a <em>TaxonomyFieldValue<\/em> object and call it&#8217;s <em>PopulateFromLabelGuidPair<\/em> method, but again, this does not appear to consistently update the taxonomy field.<\/p>\n<p>Obviously the code requires error handling, but hopefully it will give someone a base to work with.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There appears to be various different recommendations of ways for updating a term store field value and I&#8217;d like to chip in with mine. Some recommend updating by setting the list item&#8217;s hidden taxonomy field, which looks something like the &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2010\/11\/19\/updating-term-store-field-value\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[3,8],"tags":[81,83],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-2b","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/135"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":13,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"predecessor-version":[{"id":147,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/135\/revisions\/147"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}