Tell me more ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

How do I bind termstore to treeview webpart? I want to show my term store in a webpart. I was using the code below but its giving away all the groups but I want the one which I have selected only.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace Vin.Sharepoint.TermsWebpart1.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SPSite thisSite = SPContext.Current.Site;
            TaxonomySession session = new TaxonomySession(thisSite);
            TreeNode treeNode = new TreeNode();
            treeNode.Text = "Accounting and Finance";
            MetadataTreeView.Nodes.Add(treeNode);

            foreach (TermStore termStore in session.TermStores)
            {

                var tsNode = new TreeNode(termStore.Name, null, null, "", null);
                treeNode.ChildNodes.Add(tsNode);

                //treeNode = tsNode;

                foreach (Group group in termStore.Groups) 
                {


                    var node = new TreeNode(group.Name, null, null, "", null);
                    treeNode.ChildNodes.Add(node);
                    //treeNode = node; 

                    foreach (TermSet termSet in group.TermSets)
                    {

                        node = new TreeNode(termSet.Name, null, null, "", null);

                        treeNode.ChildNodes.Add(node);
                        treeNode = node;
                        foreach (Term term in termSet.Terms)
                        {
                            AddTermSet(term, treeNode);

                        }
                    }
                }
            }
        }
        void AddTermSet(Term term, TreeNode treeNode)
        {
            var node = new TreeNode(term.Name, null, null, "", null);
            treeNode.ChildNodes.Add(node);
            treeNode = node;
            foreach (Term t in term.Terms)
            {
                AddTermSet(t, treeNode);
            }
        }
    }
}
share|improve this question
What do you mean by "the one which I have selected only"? How are you selecting it? – Kit Menke Oct 4 '11 at 15:39
Hi kit Menke I meant to be the termset I'm selecting from the one's I have in the hierarchyof my group. – vikky Oct 4 '11 at 17:22

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.