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 and a termstore:
TaxonomySession session = new TaxonomySession(site); //site is an SPSite object TermStore defaultKeywordStore = session.DefaultKeywordsTermStore; |
Get the default site collection TermStore associated with the provide site.
TermStore defaultSiteCollectionStore = session.DefaultSiteCollectionTermStore; TermStoreCollection termStores = session.TermStores; |
To get a term, we have a lot of options:
TermCollection terms = session.GetTerms(prefix, true, StringMatchOption.StartsWith, 4, true); // parameter 2: Only search in default labels, false does broader scan // 4: The maximum number of terms returned from each TermStore // parameter 4: The results should not contain unavailable terms |
We can even search by a custom property name:
TermCollection terms = session.GetTermsWithCustomProperty( customPropertyName, true); Terms.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]. [/sourcecode ] switching to PowerShell, here's how to add a term: [sourcecode language="powershell"] $taxonomySession = Get-SPTaxonomySession <em>-Site</em> $TaxSite $termStore = $taxonomySession.TermStores["Managed Metadata Service"] $group = $termStore.Groups["Claims"] $termSet = $group.TermSets | Where-Object { $_.Name -eq $termSetName } if($termSet -eq $null) { try { $termSet = $group.CreateTermSet($termSetName) $termStore.CommitAll() Write-Host "Created Successfully $($termSetName) TermSet" } catch { Write-Host "Whoops, could not create $($termSetName) TermSet" } $Lev1TermObj=$termSet.createterm($Lev1Term,1033) # make it available for tagging $Lev1TermObj.set_IsAvailableForTagging($true); # set a description $Lev1TermObj.SetDescription($Description,1033) [/sourcecode ] |
Renaming terms
There’s a reason why there’s no rename method in the taxonomy object model. Instead there’s a way to “move” a term. To move from one to another, make sure both are term objects, and do: [sourcecode language=”powershell”] $Lev1TermObj.Move($Lev2TermObj) [/sourcecode ] It’s overloaded to allow you to move it to the top level of a term set. Rather than “rename” 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=”powershell”] $Lev1TermObj.CreateLabel(“Joel new Term”,1033,$true)
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.
The terms can be seen:
$Lev1TermObj.GetAllLabels(1033) [/sourcecode ] There is Timer Job that runs hourly called Taxonomy Update Scheduler
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.
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.
Want to talk?
Drop us a line. We are here to answer your questions 24*7.