0

I want to get all terms including child. Currently have 3 Levels but it may increase so cannot hard code. Here is code which just give me parent terms.

string spWebUrl = SPContext.Current.Web.Url;
        using (SPSite site = new SPSite(spWebUrl))
        {
           TaxonomySession taxonomySession = new TaxonomySession(site);

           //get the default one
            TermStore termStore = taxonomySession.TermStores["OrganisationChart"];
           /* or you can get the one you want by looping through all the term stores
             taxonomySession.TermStores
           */

           var group = termStore.Groups["MyGroup"];

           TermSet termSet = group.TermSets["MyTermSet"];


            TermCollection terms = termSet.Terms;

            foreach (var t in terms)
            {
                Console.Write(t.Name);

            }

        }

Is it also possible to store this information in list on fly?

Update1 I dont need flat list by using GetAllTerms.

1

If you need just a flat list of all terms inside a TermSet you can use GetAllTerms() method.

TermCollection terms = termSet.GetAllTerms();

foreach (var t in terms)
{
    Console.Write(t.Name);

}

EDIT: If you want to preserve the hierarchy , you have to loop through each term as below:

TermSet termSet = group.TermSets["MyTermSet"];
TermCollection terms = termSet.Terms;
foreach (var t in terms)
{
   GetChildTerms(t);
}

Method implemented as below:

public void GetChildTerms(Term parentTerm)
{
    Console.Write(parentTerm.Name);

    if (parentTerm.TermsCount>0)
    {
       foreach (var t in parentTerm.Terms)
       {
           GetChildTerms(t);
       }
    }
}
  • i dont need flat list. I am very much aware about getallterms – Happy Oct 27 '15 at 11:18
  • also updated question – Happy Oct 27 '15 at 11:20
  • how to search specific term set from Group? – Happy Oct 27 '15 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.