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):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 inSharePoint 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
Namespaces
So, the first thing we would like to do is to learn how we can read the taxonomieswe’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:
- Microsoft.SharePoint.Taxonomy.TaxonomySession
- Microsoft.SharePoint.Taxonomy.TermStore
- Microsoft.SharePoint.Taxonomy.Group
- Microsoft.SharePoint.Taxonomy.TermSet
- Microsoft.SharePoint.Taxonomy.Term
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 inthe 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" ;
tvMetadataTree.Nodes.Add(treeNode);
foreach (TermStore termStore in session.TermStores)
{
var tsNode = new TreeNode (termStore.Name, null , null
, "" , null );
treeNode.ChildNodes.Add(tsNode);
//treeNode = tsNode;
foreach (Group group in termStore.Groups)
{
var node = new TreeNode (group.Name, null , null ,
"" , null );
treeNode.ChildNodes.Add(node);
//treeNode = node;
foreach (TermSet termSet in group.TermSets)
{
node = new TreeNode (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 = new TreeNode (term.Name, null , null ,
"" , null );
treeNode.ChildNodes.Add(node);
treeNode = node;
foreach (Term t in term.Terms)
{
AddTermSet(t, treeNode);
}
}
}
looking something like this:
That’s about what you would need to get started with this!
Have you ever thought about writing an e-book or guest authoring on other
ReplyDeletewebsites? I have a blog centered on the same topics you discuss
and would really like to have you share some stories/information.
I know my audience would value your work. If you are even remotely interested, feel free to shoot
me an e-mail.
My page - alojamento web ()