SP 2010: Introduction to programmatically working with Taxonomies in SharePoint Server 2010
Posted by Tobias Zimmergren
One of the coolest new set of functionality for SharePoint 2010 is the Taxonomies (Term Store, Term Sets, Terms) that you can easily create using the amazing Managed Metadata Manager service application.
In this article I’ll talk briefly about how you can utilize the SharePoint API to programmatically work with Taxonomies and create terms and fetch the terms in your term store. This should give you some nice ideas on how to get going!
A simple example of a taxonomy
Let’s say we’ve got a taxonomy worked out and implemented in SharePoint. It could look something like this (I’m using some made up samples below):
So, if you’ve got a taxonomy configured in your Managed Metadata Service Application, you can work with those programmatically – and that’s what this little tip is about.
Work with the taxonomy API’s programmatically in SP 2010
In this article I will talk about some of the basics to get started with taxonomies in SharePoint 2010 programmatically. First, of course, we need to create a new project and add the references for the Taxonomy API.
Preparing for development
First of all, create a new project (In my case, I chose to create a Visual Web Part project).
You’ll need to add the following reference to your project:
You’ll find this reference here:
C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14ISAPIMicrosoft.SharePoint.Taxonomy.dll
So, the first thing we would like to do is to learn how we can read the taxonomies we’ve got in our store. To do this, we utilize Microsoft.SharePoint.Taxonomy.
There’s a few good-to-know classes in this namespace that we’re going to work with:
The above classes are stated in their hierarchically correct order, meaning means that you start out with the TaxonomySession which contains the TermStore, which contains the Groups.. and so on.
Reading the Metadata store (Managed Metadata Service) programmatically
Sample code from my Visual Web Part’s user control (I have a control called tvMetadataTree in the user control):
public partial class TaxonomyWebPartUserControl : UserControl
protected void Page_Load(object sender, EventArgs e)
SPSite thisSite = SPContext .Current.Site;
TaxonomySession session = new TaxonomySession (thisSite);
TreeNode treeNode = new TreeNode ();
treeNode.Text = "Metadata Awesomeness" ;
foreach (TermStore termStore in session.TermStores)
var tsNode = new TreeNode (termStore.Name, null , null , "" , null );
//treeNode = tsNode;
foreach (Group group in termStore.Groups)
var node = new TreeNode (group.Name, null , null , "" , null );
//treeNode = node;
foreach (TermSet termSet in group.TermSets)
node = new TreeNode (termSet.Name, null , null , "" , null );
treeNode = node;
foreach (Term term in termSet.Terms)
void AddTermSet(Term term, TreeNode treeNode)
var node = new TreeNode (term.Name, null , null , "" , null );
treeNode = node;
foreach (Term t in term.Terms)
The end result will be a simple TreeView control filled with the Metadata structure from the store, looking something like this:
That’s about what you would need to get started with this!
Alright, so I wrote this small article up due to some students asked me for some taxonomy sample code a while back. I hope everyone enjoys this tip on how to programmatically work with the Taxonomies in SharePoint Server 2010!
As always, enjoy!