Skip to main content

Cloud

How to create Term Store groups and Term Sets in C#

After being shown the Managed Metadata Service and the Term Stores in SharePoint 2010, I felt the need to figure out how to create all of the items that were shown to me in code. Below is a list of new classes that we will be using to accomplish the task:
  • TaxonomySession
  • TermStore
  • Group
  • TermSet

The code below will create a new group called “A Group” and then create a TermSet and populating the TermSet with items. Here is the code:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = new SPSite(http://sp2010);
TaxonomySession ts = new TaxonomySession(site);
TermStore termStore = ts.TermStores[1];
Group group = termStore.CreateGroup(“A Group”);
TermSet termSet = group.CreateTermSet(“Term Set 1”);
termSet.CreateTerm(“Term 1”, 1033);
termSet.CreateTerm(“Term 2”, 1033);
termSet.CreateTerm(“Term 3”, 1033);
termStore.CommitAll();
});

Here is what the final output looks like when looking at it in Central Administration:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.