Accessing Term Sets using SharePoint 2010 object model


Note: This post is based on Beta 2 and is subject to change in future releases.


So this afternoon I was working on a project that required accessing term sets stored in a SharePoint 2010 managed metadata service application, in order to have access to term sets using OM you can use an instance of Microsoft.SharePoint.Taxonomy.TaxonomySession and reference all the term sets and terms within the term store. Following snippet shows how to do this:


   1:   static void TraverseTermSet(TermCollection tc)
   2:          {
   3:              foreach (Term t in tc)
   4:              {
   5:                  Console.WriteLine("Parent:{0} - {1}", (t.IsRoot ? "Root" : t.Parent.Name), t.Name);
   6:                  if (t.Terms != null)
   7:                      TraverseTermSet(t.Terms);
   8:              }
   9:          }
  10:          static void Main(string[] args)
  11:          {
  12:              
  13:              
  14:                  using (SPSite sc = new SPSite("http://intranet.litwareinc.com"))
  15:                  {
  16:                      Microsoft.SharePoint.Taxonomy.TaxonomySession session = new Microsoft.SharePoint.Taxonomy.TaxonomySession(sc);
  17:                      TermStore termstore = session.TermStores["Managed Metadata Service"];
  18:                      Group group = termstore.Groups["Litware Corporate Taxonomy"];
  19:                      TermSet termset = group.TermSets["Segment"];
  20:                      TraverseTermSet(termset.Terms);
  21:                  }
  22:                  Console.Read();
  23:              
  24:          }

 


 


MSS2010PS16 Jan. 06 20.41

Comments (0)

Skip to main content