Taxonomy / Managed Metadata Service: Get Taxonomy Termsets by Code

Here is a small snippet to get a TermSet out of the Managed Metadata Service from SharePoint 2010

03 January 2011
Christoph Keller Christoph Keller

Here is a small snippet to get a TermSet out of the Managed Metadata Service from SharePoint 2010:

string siteUrl = "http://SITEURL";
using (SPSite localSite = new SPSite(siteUrl))
{
    // Create Taxonomy Session
    TaxonomySession taxSession = new TaxonomySession(localSite);
 
    // Get the 'Managed Metadata Service' TermStore
    TermStore taxStore = taxSession.TermStores["Managed Metadata Service"];
 
    // Get the 'UserProperties' Taxonomy Group
    Group taxGroup = taxStore.Groups["UserProperties"];
 
    // Get the 'TermSetName' Termset
    TermSet taxSet = taxGroup.TermSets["TermSetName"];
 
    foreach (Term term in taxSet.GetAllTerms())
    {
        string termDescription = term.GetDescription();
        if (String.IsNullOrEmpty(termDescription))
        {
            termDescription = "No description available";
        }
 
        Console.WriteLine("Term {0}: {1}", term.Name, termDescription);
    }
}

comments powered by Disqus