locked
Retrieve Term store, Term Group and Term sets based on managed metadata column RRS feed

  • Question

  • Hi, 

    Is it possible to retrieve associated Term store name, Term group name and term set name based on a managed metadata column value?

    I am looking at server side code and not CSOM.

    'Thanks!

    Monday, September 17, 2012 4:04 AM

Answers

  • Hi,

    Please try this console application that does work for me:

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Taxonomy;
    using System;
    using System.Linq;
    
    namespace ListConsoleApplication
    {
        class Program
        {
            static TermStore GetTermStore(SPSite site, Guid termStoreId)
            {
                // Get a TaxonomySession from the site
                TaxonomySession session = new TaxonomySession(site);
                TermStore termStore = null;
                // Get a TermStore from the session
                if (session.TermStores != null)
                {
                    termStore = session.TermStores[termStoreId];
                }
                return termStore;
            }
            static void Main(string[] args)
            {
                using (SPSite oSite = new SPSite("http://perseus/"))
                {
                    using (SPWeb oWebsiteRoot = oSite.OpenWeb())
                    {
                        string listName = "CustomList";
                        string fieldName = "TermColumn";
                        SPList oList = oWebsiteRoot.Lists[listName];
                        TaxonomyField oField = (TaxonomyField)oList.Fields[fieldName];
                        TermStore termStore = GetTermStore(oSite, oField.SspId);
                        foreach( Group g in termStore.Groups)
                        {
                            TermSet termSet = g.TermSets.FirstOrDefault(t => t.Id == oField.TermSetId);
    
                            if (termSet != null)
                            {
                                Console.WriteLine("Term store is {0}", termStore.Name);
                                Console.WriteLine("Term group is {0}", g.Name);
                                Console.WriteLine("Term set is {0}", termSet.Name);
    
                                break;
                            }
                        }
                    }
                }
            }
        }
    }


    Dmitry

    Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts | Lightning Tools Blog

    • Proposed as answer by Amit SP Monday, September 17, 2012 3:23 PM
    • Marked as answer by battlesmith Monday, September 17, 2012 11:59 PM
    Monday, September 17, 2012 11:19 AM

All replies

  •      SPSite site = SPContext.Current.Site;
           SPWeb web = site.OpenWeb();
    
    
            TaxonomySession session = new TaxonomySession(site);
    
            TermStore termStore = session.TermStores[termStoreName];
    
        Group group = termStore.Groups[termGroupName];
     
        TermSet taxTerm = group.TermSets[termSetsName];
    I am using above code and I pass the values for termStoreName, termGroupName and termSetsName. But I would like to retrieve these value which are associated with a managed metadata field.
    Monday, September 17, 2012 5:40 AM
  • Hi,

    Please try this console application that does work for me:

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Taxonomy;
    using System;
    using System.Linq;
    
    namespace ListConsoleApplication
    {
        class Program
        {
            static TermStore GetTermStore(SPSite site, Guid termStoreId)
            {
                // Get a TaxonomySession from the site
                TaxonomySession session = new TaxonomySession(site);
                TermStore termStore = null;
                // Get a TermStore from the session
                if (session.TermStores != null)
                {
                    termStore = session.TermStores[termStoreId];
                }
                return termStore;
            }
            static void Main(string[] args)
            {
                using (SPSite oSite = new SPSite("http://perseus/"))
                {
                    using (SPWeb oWebsiteRoot = oSite.OpenWeb())
                    {
                        string listName = "CustomList";
                        string fieldName = "TermColumn";
                        SPList oList = oWebsiteRoot.Lists[listName];
                        TaxonomyField oField = (TaxonomyField)oList.Fields[fieldName];
                        TermStore termStore = GetTermStore(oSite, oField.SspId);
                        foreach( Group g in termStore.Groups)
                        {
                            TermSet termSet = g.TermSets.FirstOrDefault(t => t.Id == oField.TermSetId);
    
                            if (termSet != null)
                            {
                                Console.WriteLine("Term store is {0}", termStore.Name);
                                Console.WriteLine("Term group is {0}", g.Name);
                                Console.WriteLine("Term set is {0}", termSet.Name);
    
                                break;
                            }
                        }
                    }
                }
            }
        }
    }


    Dmitry

    Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts | Lightning Tools Blog

    • Proposed as answer by Amit SP Monday, September 17, 2012 3:23 PM
    • Marked as answer by battlesmith Monday, September 17, 2012 11:59 PM
    Monday, September 17, 2012 11:19 AM