Print Shortlink

How to: access SharePoint 2010 managed metadata store items

Whether you’re using managed metadata store values in your customization or just as metadata properties for list items, in larger environments you will likely need to come up with an automated way of provisioning the metadata and in some cases accessing it.

In this post, we’ll take a look at how you can read managed metadata store values as a part of the sample web part:

Ensure you’re referencing Microsoft..Taxonomy assembly in your Visual Studio project.
Next, ensure you’ve referenced the Taxonomy namespace

using Microsoft..Taxonomy;

Now, you can access values of the metadata store the following way; in this case we’re storing them in the table:

private static DataTable RetrieveManagedMetadataItems(SPSite site)
{
DataTable items = new DataTable();
TaxonomySession session = new TaxonomySession(site);
TermStore termStore = session.TermStores["Managed Metadata Service"]; TermSetCollection = termStore.GetTermSets("My term set", 1033);

if (termSets.Count > 0)
{
for (int i = 0; i < termSets[0].Terms.Count; i++)
{
items.Rows.Add(termSets[0].Terms[i].Name,
  termSets[0].Terms[i].GetDescription(),);
}
}
return items;
}

The value of Managed Metadata Service may be different in your environment, take a note of that if you get errors at the following line:

TermStore termStore = session.TermStores["Managed Metadata Service"];

Then you connect to a term set containing terms. In our case the term set is named My term set; 1033 is a language ID, English by default.

Finally, the data is stored in the data table.

Enjoy!