Get the Taxonomy structure into a Asp.Net TreeView
Enjoy ;)
protected void Page_Load(object sender, EventArgs e)
{
SPSite thisSite = SPContext .Current.Site;
TaxonomySession session = new TaxonomySession (thisSite);
TreeNode treeNode = new TreeNode ();
treeNode.Text = "Root" ;
tvMetadataTree.Nodes.Add(treeNode);
int indexTermStoreCount = 0;
foreach (TermStore termStore in session.TermStores)
{
var tsNode = new TreeNode(termStore.Name, indexTermStoreCount.ToString(), null, "", null);
treeNode.ChildNodes.Add(tsNode);
foreach (Group group in termStore.Groups)
{
if (!group.IsSiteCollectionGroup)
{
TreeNode node = new TreeNode(group.Name, null, null, "", null);
tvMetadataTree.Nodes[0].ChildNodes[Convert.ToInt16(indexTermStoreCount)].ChildNodes.Add(node);
int indexGroupCount = 0;
foreach (TermSet termSet in group.TermSets)
{
TreeNode ts = new TreeNode(termSet.Name, indexGroupCount.ToString(), null, "", null);
tvMetadataTree.Nodes[0].ChildNodes[Convert.ToInt16(indexTermStoreCount)].ChildNodes[Convert.ToInt16(indexGroupCount)].ChildNodes.Add(ts);
ts.Selected = true;
foreach (Term term in termSet.Terms)
{
AddTermSet(term, ts);
}
}
indexGroupCount++;
}
}
indexTermStoreCount++;
}
}
protected void AddTermSet(Term term, TreeNode treeNode)
{
TreeNode childNode = new TreeNode(term.Name, null, null, "", null);
treeNode.ChildNodes.Add(childNode);
foreach (Term t in term.Terms)
{
AddTermSet(t, childNode);
}
}
No comments:
Post a Comment