How to Get all Terms from the TermStore in SharePoint Using C#

Introduction

 

This post shows how to retrieve all the Terms from the Term Store in SharePoint Using C#We are fetching a custom Term Set from a Custom Group.


Code:

  1. using (SPSite RootSite = new SPSite(SPContext.Current.Site.Url))      
  2.             {      
  3.                 using (SPWeb RootWeb = RootSite.OpenWeb())      
  4.                 {      
  5.                     TaxonomySession TaxonomySession = new TaxonomySession(RootSite);      
  6.                     TermStore termstore = TaxonomySession.TermStores["Managed Metadata Service"];      
  7.                     Group group = termstore.Groups["CustomGroup"];      
  8.                     TermSet termset = group.TermSets["CustomTermset"];     
  9.                     TermCollection termcoll = termset.GetAllTerms();      
  10.                     StringBuilder strbl = new StringBuilder();      
  11.                     foreach (Term term in termcoll)      
  12.                     {      
  13.                         strbl.Append(term.Name.ToString());                              
  14.                     }      
  15.                     Literal1.Text = strbl.ToString();      
  16.               }      
  17.             }