Managed metadata is a hierarchical collection of centrally managed terms that you can define, and then use as attributes for items in Microsoft SharePoint Server 2010. It’s an ideal approach of centrally managing all your metadata, but there has to be content before it can be managed.
Filling the termstore manually can be a boring and cumbersome task. I will explain how the termstore can be provisioned using code. I found that the library used was/is not properly documented and I hope that it will be in the future. More information about the taxonomy namespace can be found on MSDN.
Fire up Visual Studio 2010 and create a new project. The next step is adding the taxonomy dll. Right click on the references folder inside your project and add a new one:
Select the .NET tab and sort on the component name. Scroll down to Microsoft.SharePoint.Taxonomy and click OK. Make sure you selected the Taxonomy.dll and not Taxonomy.intl.dll:
The reference should now be added:
Finally, add the following line at the using statements in your code file to enable taxonomy controls:
using Microsoft.SharePoint.Taxonomy;
Now that everything is set, we can start adding code to our project. Start out with creating a SPSite object:
SPSite oSite = SPContext.Current.Site;
Or reference a url directly by writing a using block:
using (SPSite oSite = new SPSite("http://sharepoint/")) { }
Create a new TaxonomySession. This will wrap up all of the associated TermStore objects for the SPSite object.
TaxonomySession oTaxSession = new TaxonomySession(oSite);
With a session created, we can create our TermStore object.
TermStore oTermStore = oTaxSession.DefaultSiteCollectionTermStore;
There’s a misconfiguration if you receive a null object from the DefaultSiteCollectionTermStore, which can be easily fixed.
A specific TermStore can also be referenced by using the TermStores collection of the TaxonomySession object:
TermStore oTermStore = oTaxSession.TermStores[""]
With the TermStore object created, a new group can be added with the CreateGroup() method:
Group oTermGroup = oTermStore.CreateGroup("New Group"); oTermGroup.Description = "New Group Description";
Inside this taxonomy group, a new TermSet with Terms can be added:
TermSet oTermSet = oTermGroup.CreateTermSet("New TermSet"); oTermSet.Description = "New TermSet Description";
Term oTerm = oTermSet.CreateTerm("New Term", oTermStore.DefaultLanguage); oTerm.SetDescription("New Term Description", oTermStore.DefaultLanguage); oTerm.CreateLabel("New Term Label", oTermStore.DefaultLanguage, false);
Instead of the oTermStore.DefaultLanguage, locale id’s can be used (e.g. 1033 for English).
So far we created a new group and added a new termset where a new term was created. All these changes are pending currently in the pending status. Use the CommitAll() method of the TermStore object to save the changes permanently:
oTermStore.CommitAll();
If you wish to discard any changes that are pending, use the RollbackAll() method:
oTermStore.RollbackAll();
When the code is executed, we can find our result back in the Term Store Management Tool:
To check whether an object already exists in the term store, I use the following method:
try { oTermGroup = oTermStore.Groups["New Group"]; } catch (ArgumentOutOfRangeException ex) { oTermGroup = oTermStore.CreateGroup("New Group"); oTermGroup.Description = "New Group Description"; }
In summary, the code file should look like this:
TaxonomySession oTaxSession; TermStore oTermStore; Group oTermGroup; TermSet oTermSet; Term oTerm; private void AddFieldToTaxonomy() { SPSite oSite = SPContext.Current.Site; oTaxSession = new TaxonomySession(oSite); oTermStore = oTaxSession.DefaultSiteCollectionTermStore; try { oTermGroup = oTermStore.Groups["New Group"]; } catch (ArgumentOutOfRangeException ex) { oTermGroup = oTermStore.CreateGroup("New Group"); oTermGroup.Description = "New Group Description"; } try { oTermSet = oTermGroup.TermSets["New TermSet"]; } catch (ArgumentOutOfRangeException ex) { oTermSet = oTermGroup.CreateTermSet("New TermSet"); oTermSet.Description = "New TermSet Description"; } try { oTerm = oTermSet.Terms["New Term"]; } catch (ArgumentOutOfRangeException ex) { oTerm = oTermSet.CreateTerm("New Term", oTermStore.DefaultLanguage); oTerm.SetDescription("New Term Description", oTermStore.DefaultLanguage); oTerm.CreateLabel("New Term Label", oTermStore.DefaultLanguage, false); } oTermStore.CommitAll(); }
Happy metadata provisioning!
You must be logged in to post a comment.