SharePoint C# Read Managed Metadata Service information
By Jatin Prajapati
In this FAQ we will see how to read Managed Metadata information in SharePoint 2010 site collection, using C# and SharePoint Object model.
Following is the code for getting Managed Metadata information in context to Site
collection.
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 TestSharePointProject.ManagedMetaDataInformationWebPart
{
public partial class ManagedMetaDataInformationWebPartUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadManagedMetaDataInformation();
}
}
private void LoadManagedMetaDataInformation()
{
TaxonomySession session = new TaxonomySession(SPContext.Current.Site);
TermStoreCollection termstores = session.TermStores;
foreach (TermStore store in termstores)
{
TreeNode node = new TreeNode();
node.Text = store.Name;
GetGroups(node, store.Groups);
treeView.Nodes.Add(node);
}
}
private void GetGroups(TreeNode node, GroupCollection groupCollection)
{
foreach (Group g in groupCollection)
{
TreeNode child = new TreeNode();
child.Text = g.Name;
GetTermSets(child, g.TermSets);
node.ChildNodes.Add(child);
}
}
private void GetTermSets(TreeNode node, TermSetCollection termSetCollection)
{
foreach (TermSet ts in termSetCollection)
{
TreeNode child = new TreeNode();
child.Text = ts.Name;
GetTermSets(child, ts.Terms);
node.ChildNodes.Add(child);
}
}
private void GetTermSets(TreeNode node, TermCollection termCollection)
{
foreach (Term ts in termCollection)
{
TreeNode child = new TreeNode();
child.Text = ts.Name;
node.ChildNodes.Add(child);
}
}
}
}
Download the full working code from here.
SharePoint C# Read Managed Metadata Service information (3793 Views)