{"id":1861,"date":"2012-05-16T05:23:36","date_gmt":"2012-05-16T05:23:36","guid":{"rendered":"https:\/\/poiseddevelopers.com\/reality-tech\/?p=1861"},"modified":"2024-05-13T10:43:03","modified_gmt":"2024-05-13T10:43:03","slug":"taxonomy-internals-deep-dive","status":"publish","type":"post","link":"https:\/\/poiseddevelopers.com\/reality-tech\/taxonomy-internals-a-rose-by-any-other-name\/","title":{"rendered":"Taxonomy Internals Deep Dive"},"content":{"rendered":"<p>The SharePoint Managed Metadata Service centrally manages Taxonomies within SharePoint. More than one service application can exist within a farm. Each uses the traditional service connection proxy to associate with a web application.<\/p>\n<p>Each service application has its own database where terms are stored.<\/p>\n<p>To navigate using the object model, first we grab a session and a termstore:<\/p>\n<p>&nbsp;<\/p>\n<pre lang=\"php\"> TaxonomySession session = new TaxonomySession(site); \/\/site is an SPSite object\r\nTermStore defaultKeywordStore = session.DefaultKeywordsTermStore;\r\n<\/pre>\n<p>Get the default site collection TermStore associated with the provide site.<\/p>\n<p>&nbsp;<\/p>\n<pre lang=\"php\"> TermStore defaultSiteCollectionStore = session.DefaultSiteCollectionTermStore;\r\nTermStoreCollection termStores = session.TermStores;<\/pre>\n<p>To get a term, we have a lot of options:<\/p>\n<p>&nbsp;<\/p>\n<pre lang=\"php\">TermCollection terms = session.GetTerms(prefix, true, StringMatchOption.StartsWith, 4, true);\r\n\/\/ parameter 2: Only search in default labels, false does broader scan\r\n\/\/ 4: The maximum number of terms returned from each TermStore\r\n\/\/ parameter 4: The results should not contain unavailable terms<\/pre>\n<p>We can even search by a custom property name:<\/p>\n<pre lang=\"php\">  TermCollection terms = session.GetTermsWithCustomProperty( customPropertyName, true);\r\nTerms.count() will give you the total terms returned.  If you know you are doing a precise lookup, then the term you are looking for is found in terms[0].\r\n[\/sourcecode ]\r\n \r\nswitching to PowerShell, here's how to add a term:\r\n[sourcecode language=\"powershell\"]\r\n $taxonomySession = Get-SPTaxonomySession <em>-Site<\/em> $TaxSite\r\n $termStore = $taxonomySession.TermStores[\"Managed Metadata Service\"]\r\n $group = $termStore.Groups[\"Claims\"]\r\n \r\n $termSet = $group.TermSets | Where-Object { $_.Name -eq $termSetName }\r\nif($termSet -eq $null)\r\n     {\r\n          try\r\n          {\r\n              $termSet = $group.CreateTermSet($termSetName) \r\n              $termStore.CommitAll()\r\n              Write-Host \"Created Successfully $($termSetName) TermSet\"\r\n          }\r\n          catch\r\n          {\r\n              Write-Host \"Whoops, could not create $($termSetName) TermSet\"\r\n          }\r\n \r\n$Lev1TermObj=$termSet.createterm($Lev1Term,1033)\r\n# make it available for tagging\r\n$Lev1TermObj.set_IsAvailableForTagging($true);\r\n# set a description\r\n$Lev1TermObj.SetDescription($Description,1033)\r\n[\/sourcecode ]<\/pre>\n<h2>Renaming terms<\/h2>\n<p>There&#8217;s a reason why there&#8217;s no rename method in the taxonomy object model. Instead there&#8217;s a way to &#8220;move&#8221; a term. To move from one to another, make sure both are term objects, and do: [sourcecode language=&#8221;powershell&#8221;] $Lev1TermObj.Move($Lev2TermObj) [\/sourcecode ] It&#8217;s overloaded to allow you to move it to the top level of a term set. Rather than &#8220;rename&#8221; a term, instead a new label is applied to the term, and the new label can be made the default value. In this case, I added a new label for the term, then made it the default for our language (1033): [sourcecode language=&#8221;powershell&#8221;] $Lev1TermObj.CreateLabel(&#8220;Joel new Term&#8221;,1033,$true)<\/p>\n<p>Note the UI does not let you set a new term as the default. Your only option is to exchange the two values of the labels.<\/p>\n<p>The terms can be seen:<\/p>\n<pre data-uw-rm-sr=\"\">$Lev1TermObj.GetAllLabels(1033)\r\n[\/sourcecode ]\r\nThere is Timer Job that runs hourly called Taxonomy Update Scheduler<\/pre>\n<p>This updates Site Collections with the latest term changes made to the Enterprise Metadata Service. The amazing thing is this Timer Job updates the term, even if a document is checked out. The document remains checked out, but the term value changes.<\/p>\n<p>The wonderful thing about the approach to add a default label rather than rename a term is that the user can find the term searching for any label, yet it is the default label that will appear to the user in the user interface.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The SharePoint Managed Metadata Service centrally manages Taxonomies within SharePoint. More than one service application can exist within a farm. Each uses the traditional service connection proxy to associate with a web application. Each service application has its own database where terms are stored. To navigate using the object model, first we grab a session [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":1848,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[28],"tags":[],"class_list":["post-1861","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"acf":[],"_links":{"self":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1861","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/comments?post=1861"}],"version-history":[{"count":3,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1861\/revisions"}],"predecessor-version":[{"id":3953,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1861\/revisions\/3953"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media\/1848"}],"wp:attachment":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media?parent=1861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/categories?post=1861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/tags?post=1861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}