Managed metadata is a hierarchical collection of centrally managed terms that you can define, and then use as attributes for items in Microsoft SharePoint Server 2010. Refer here for more information on the managed metadata in SharePoint 2010.
A column is a location in a list in which to store information about a SharePoint Server 2010 item. When you define a column, you provide a name for the column, specify the column's type, and provide additional information that depends on the column type.
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:
- Create a SharePoint 2010 project
- Add a feature and feature receiver
- Add code to create a managed metadata site column and then add the site column to a custom content type
- Deploy the solution to SharePoint 2010
The code in this topic creates a TaxonomyField class which was introduced in SharePoint 2010. The TaxonomyField class is a custom field class that inherits from the SPFieldLookUp class. Refer here for more on the TaxonomyField class.
Programmatically Create the Managed Metadata Column
The following steps deploy the code to SharePoint 2010 as a feature receiver that programmatically creates the managed metadata column. The procedures in this section assume a development environment where SharePoint 2010 is installed and configured, Microsoft Visual Studio 2010 is installed, and the currently logged-in user has administrative rights on the SharePoint environment for deployment purposes.
Before you begin, ensure that you have the Managed Metadata service provisioned and Term sets and terms created. Figure 1 shows the terms set and terms.
Figure 1. Term set and terms
1. In Visual Studio 2010, click New Project, expand the SharePoint node, click 2010, and then click Empty SharePoint Project. Provide name for the project.
2. In the SharePoint Customization Wizard, select the local SharePoint site that will be used for debugging. Select the Deploy as a farm solution option and then click Finish.
Note: Creating a TaxonomyField class requires a reference to the Microsoft.SharePoint.Taxonomy.dll. This .dll is not available in a sandbox solution and therefore the solution will have to be deployed as a farm solution.
3. In Solution Explorer, right-click the Features node, and then click Add Feature as shown in Figure 2.
Figure 2. Add the feature
4. Name the feature AddMMSColumn and add a description as shown in Figure 3. Change the Scope to Site.
Figure 3. Define the feature
5. Right click the AddMMSColumn feature node and then click Add Event Receiver. (see Figure 4).
Figure 4. Add an event receiver
6. Add the following code in the feature receiver’s FeatureActivated event.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
const string TermStoreName = "Managed Metadata Service";
const string GroupName = "Office Locations";
const string TermSetName = "Europe";
const string SPLocationListColumn = "OfficeLocation";
const string ContentTypeOfficeDetails = "Office Details";
//Get the Taxonomy session for current SPSite
TaxonomySession session = new TaxonomySession(SPContext.Current.Site);
if (session.TermStores.Count != 0)
using (SPSite site = (SPSite)properties.Feature.Parent)
SPWeb rootweb = site.RootWeb;
var termStore = session.TermStores[TermStoreName];
var group = from g in termStore.Groups where g.Name == GroupName select g;
var termSet = group.FirstOrDefault().TermSets[TermSetName];
//Get the content type to which the taxonomy field is to be added
SPContentType contentTypeOfficeDetails = rootweb.ContentTypes[ContentTypeOfficeDetails];
//Check if field exists, if yes take no action
bool fieldExists = rootweb.Fields.ContainsField(SPLocationListColumn);
//Create a new TaxonomyField
TaxonomyField field = rootweb.Fields.CreateNewField("TaxonomyFieldType", SPLocationListColumn) as TaxonomyField;
field.SspId = termSet.TermStore.Id;
field.TermSetId = termSet.Id;
field.TargetTemplate = string.Empty;
field.AllowMultipleValues = true;
field.CreateValuesInEditForm = true;
field.Open = true;
field.AnchorId = Guid.Empty;
field.Group = "Taxonomy Fields";
//Add the taxonomy field to site columns
TaxonomyField fieldAdded = rootweb.Fields[SPLocationListColumn] as TaxonomyField;
//Add the field to the a custom content type
SPFieldLink fieldLnkArticle = new SPFieldLink(fieldAdded);
7. Right click the project name in Visual Studio and then click Deploy as shown in Figure 5.
Figure 5. Deploy the solution
8. Navigate to the site where the feature is activated. Click Site Settings and then click Site columns. The newly created column is visible in the Taxonomy Fields group as shown in Figure 6.
Figure 6. The newly create column
9. Click the column name to go to the Edit page of the column. The term set that was specified in the code should be selected as shown in Figure 7.
Figure 7. The highlighted term set
10. Click Site Settings and then click Site content types. Click the content type to which the column was added. The column is visible in the Manage content type page of the selected content type (see Figure 8).
Figure 8. The new column
You can start using this content type or the column in your lists and libraries.