SharePoint Server 2010 introduces a new column type named managed metadata. When you create a managed metadata column, you specify the term set from which the column's values must come. When you want users to provide information for list items (including documents), and the valid values for the information are contained in a term set, use a managed metadata column. Create a new content type or modify an existing content type, and then add the managed metadata column to the content type.
This post describes the way of programmatically creating a managed metadata taxonomy column and adding the column to a custom content type. In this article, you:
1- Create a SharePoint 2010 project
2- Add reference of "Microsoft.SharePoint.Taxonomy" assembly to your project and include namespace "Microsoft.SharePoint.Taxonomy".
3- Use below code.
using (SPSite site = new SPSite("http://sharepointserver/"))
{
TaxonomySession _TaxonomySession = new TaxonomySession(site);
//Get instance of the Term Store
TermStore _TermStore = _TaxonomySession.TermStores["My Term Store"];
//Now create a new Term Group
Group _Group = _TermStore.CreateGroup("My New Group");
//Create a new Term Set in the new Group
TermSet _TermSet = _Group.CreateTermSet("My New Termset");
//Add terms to the term set
Term _term1 = _TermSet.CreateTerm("First Term", 1033);
Term _term2 = _TermSet.CreateTerm("Second Term", 1033);
Term _term3 = _TermSet.CreateTerm("Third Term", 1033);
Term _term4 = _TermSet.CreateTerm("Fourth Term", 1033);
//commit changes
_TermStore.CommitAll();
}
This post describes the way of programmatically creating a managed metadata taxonomy column and adding the column to a custom content type. In this article, you:
1- Create a SharePoint 2010 project
2- Add reference of "Microsoft.SharePoint.Taxonomy" assembly to your project and include namespace "Microsoft.SharePoint.Taxonomy".
3- Use below code.
using (SPSite site = new SPSite("http://sharepointserver/"))
{
TaxonomySession _TaxonomySession = new TaxonomySession(site);
//Get instance of the Term Store
TermStore _TermStore = _TaxonomySession.TermStores["My Term Store"];
//Now create a new Term Group
Group _Group = _TermStore.CreateGroup("My New Group");
//Create a new Term Set in the new Group
TermSet _TermSet = _Group.CreateTermSet("My New Termset");
//Add terms to the term set
Term _term1 = _TermSet.CreateTerm("First Term", 1033);
Term _term2 = _TermSet.CreateTerm("Second Term", 1033);
Term _term3 = _TermSet.CreateTerm("Third Term", 1033);
Term _term4 = _TermSet.CreateTerm("Fourth Term", 1033);
//commit changes
_TermStore.CommitAll();
}