none
Accessing the hierarchy of terms in the term store RRS feed

  • Question

  • I was wondering if it was possible to access the hierarchy of terms in a term set or a group in the term store. I have heard it is possible but haven't been able to find a solution yet. 

    Any help here would be appreciated

    Fahad

    Tuesday, April 12, 2011 8:25 AM

Answers

  • hi Fahad ,

     

    you can get the clear idea from the following code which i was using to get the terms value, i have 4 tree view (Term Store, Groups, Term Set, Term)

     

    this is the code for populating the Term treeview, you can easily find out how to get the Term value from it.

     

     

        private void GetTerms(TreeNodeMouseClickEventArgs e)
            {
                tvTerms.Nodes.Clear();
                TaxonomySession tsession =  new TaxonomySession(Sitecollection);
                foreach (TermStore ts in tsession.TermStores)
                {
                    //string t = tvTermStore.SelectedNode.Text;
                    if (tvTermStore.SelectedNode.Text == ts.Name)
                    {

                        foreach (Group gr in ts.Groups)
                        {

                            if (gr.Name == tvGroups.SelectedNode.Text)
                            {

                                foreach (TermSet tset in gr.TermSets)
                                {

                                    if (tset.Name == e.Node.Text)
                                    {
                                   
                                        foreach(Term term in tset.Terms)
                                        {

                                          

                                            getTermsAndInnerTerms(term, 0);
                                           
                                        }
                                    }


                                }
                            }

                        }

                    }


                }

            }

     

    // this method is using to make recursive calls term value till last level

       private void getTermsAndInnerTerms(Term term, int value)
            {
                tvTerms.Nodes.Add(term.Id.ToString(), term.Name);
               
                if (term.Terms.Count>0)
                {
                    foreach (Term termChild in term.Terms)
                    {
                        getTermsAndInnerTerms(termChild, value + 1);
                   
                    }
               
                }


            }

     

    Hopefully this will clarify your query



    • Proposed as answer by Shakir majeed Tuesday, April 12, 2011 12:02 PM
    • Marked as answer by Fahad Owais Wednesday, April 13, 2011 12:12 PM
    Tuesday, April 12, 2011 12:01 PM

All replies

  • would you like to explain what you mean by "to access the hierarchy of terms in a term set or a group in the term store"?

    Tuesday, April 12, 2011 9:59 AM
  • I m sorry. I meant to ask if there was a way to do it programmatically; to get the hierarchy of terms in the term store.

    Tuesday, April 12, 2011 10:46 AM
  • hi Fahad ,

     

    you can get the clear idea from the following code which i was using to get the terms value, i have 4 tree view (Term Store, Groups, Term Set, Term)

     

    this is the code for populating the Term treeview, you can easily find out how to get the Term value from it.

     

     

        private void GetTerms(TreeNodeMouseClickEventArgs e)
            {
                tvTerms.Nodes.Clear();
                TaxonomySession tsession =  new TaxonomySession(Sitecollection);
                foreach (TermStore ts in tsession.TermStores)
                {
                    //string t = tvTermStore.SelectedNode.Text;
                    if (tvTermStore.SelectedNode.Text == ts.Name)
                    {

                        foreach (Group gr in ts.Groups)
                        {

                            if (gr.Name == tvGroups.SelectedNode.Text)
                            {

                                foreach (TermSet tset in gr.TermSets)
                                {

                                    if (tset.Name == e.Node.Text)
                                    {
                                   
                                        foreach(Term term in tset.Terms)
                                        {

                                          

                                            getTermsAndInnerTerms(term, 0);
                                           
                                        }
                                    }


                                }
                            }

                        }

                    }


                }

            }

     

    // this method is using to make recursive calls term value till last level

       private void getTermsAndInnerTerms(Term term, int value)
            {
                tvTerms.Nodes.Add(term.Id.ToString(), term.Name);
               
                if (term.Terms.Count>0)
                {
                    foreach (Term termChild in term.Terms)
                    {
                        getTermsAndInnerTerms(termChild, value + 1);
                   
                    }
               
                }


            }

     

    Hopefully this will clarify your query



    • Proposed as answer by Shakir majeed Tuesday, April 12, 2011 12:02 PM
    • Marked as answer by Fahad Owais Wednesday, April 13, 2011 12:12 PM
    Tuesday, April 12, 2011 12:01 PM
  • Awesome! It works.

    Thanks a lot Shakir....really appreciate it.

    Wednesday, April 13, 2011 12:12 PM
  • Hi ...........

    The code above is using the   Microsoft.SharePoint.Taxonomy namespace.

    Is it possible through Client object model?????????

    If possible please help me................

    Tuesday, December 13, 2011 9:35 AM