Looking for something?

Author: Tobias Zimmergren
http://www.zimmergren.net | http://www. tozit.com | [@zimmergren](http://twitter.com/zimmergren)

Introduction

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):

image

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:

image 

You’ll find this reference here:
C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14ISAPIMicrosoft.SharePoint.Taxonomy.dll

Namespaces

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):

publicpartialclassTaxonomyWebPartUserControl : UserControl

{

protectedvoid Page_Load(object sender, EventArgs e)

{

SPSite thisSite = SPContext .Current.Site;

TaxonomySession session = newTaxonomySession (thisSite);

TreeNode treeNode = newTreeNode ();

treeNode.Text = "Metadata Awesomeness" ;

tvMetadataTree.Nodes.Add(treeNode);

foreach (TermStore termStore in session.TermStores)

{

var tsNode = newTreeNode (termStore.Name, null , null , "" , null );

treeNode.ChildNodes.Add(tsNode);

//treeNode = tsNode;

foreach (Group group in termStore.Groups)

{

var node = newTreeNode (group.Name, null , null , "" , null );

treeNode.ChildNodes.Add(node);

//treeNode = node;

foreach (TermSet termSet in group.TermSets)

{

node = newTreeNode (termSet.Name, null , null , "" , null );

treeNode.ChildNodes.Add(node);

treeNode = node;

foreach (Term term in termSet.Terms)

{

AddTermSet(term, treeNode);

}

}

}

}

}

void AddTermSet(Term term, TreeNode treeNode)

{

var node = newTreeNode (term.Name, null , null , "" , null );

treeNode.ChildNodes.Add(node);

treeNode = node;

foreach (Term t in term.Terms)

{

AddTermSet(t, treeNode);

}

}

}

The end result will be a simple TreeView control filled with the Metadata structure from the store, looking something like this:

image

That’s about what you would need to get started with this!

Summary

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!

0 Comments 28 August 2010
Tobias Zimmergren

Tobias Zimmergren

Hi, I'm Tobias. I am a Microsoft MVP for SharePoint and I use this site to share my thoughts on tech with you on topics like SharePoint, Office 365, Azure and general web development.

  Malmö, Sweden

Related Post

Comments powered by Disqus
res.