Declaratively Provision Managed Metadata Column in SharePoint 2010

As part of a project I have been working on we wanted to assign categories to items in SharePoint 2010 and decided to use Managed Metadata. Wictor Wilén has a good post explaining what Managed Metadata is and how to set it up. Like Wictor, I also prefer to provision site columns and content types using a combination of declarative CAML and feature receivers. I followed the Wictors post but found he didn’t add the extra steps required to make the solution work.

I’ve outlined the steps we took to provision a managed metadata column to a site content type.

Step 1 - Create a TaxonomyField and a Note field

1<?xml version\="1.0" encoding\="utf-8" ?\> <Elements xmlns\="http://schemas.microsoft.com/sharepoint/"\>   <Field ID\="{087C759A-7BD2-4B66-9CF5-277A3399636D}"     Type\="TaxonomyFieldType"     DisplayName\="MMSCategories"     ShowField\="Term1033"     Required\="TRUE"     EnforceUniqueValues\="FALSE"     Group\="\_Custom"     StaticName\="MMSCategories"     Name\="MMSCategories"      />   <Field ID\="{437B0ED2-A31B-47F7-8C69-6B9DE2C4A4F6}"     Type\="Note"     DisplayName\="MMSCategories\_0"     StaticName\="MMSCategoriesTaxHTField0"     Name\="MMSCategoriesTaxHTField0"     ShowInViewForms\="FALSE"     Required\="FALSE"     Hidden\="TRUE"     CanToggleHidden\="TRUE"     /> </Elements\>

Step 2 - Add the TaxonomyField and Note Field to a content type

 1<?xml version\="1.0" encoding\="utf-8"?\> <Elements xmlns\="http://schemas.microsoft.com/sharepoint/"\>   <!-- Parent ContentType: Item (0x01) -->   <ContentType ID\="0x0100aa33de693811427c886a5d27f17ed23d"                Name\="Taxonomy Spike - MMSContentType"                Group\="Custom Content Types"                Description\="My Content Type"                Inherits\="TRUE"                Version\="0"\>     <FieldRefs\>       <FieldRef ID\="{437B0ED2-A31B-47F7-8C69-6B9DE2C4A4F6}" Name\="MMSCategoriesTaxHTField0"/>       <FieldRef ID\="{087C759A-7BD2-4B66-9CF5-277A3399636D}" Name\="MMSCategories"/>     </FieldRefs\>   </ContentType\> </Elements\>
 2```[](http://11011.net/software/vspaste)
 3
 4**Step 3 - Wire up the Note field to the TaxonomyField using a feature receiver**
 5
 6The TaxonomyField has a member called [TextField](http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.taxonomyfield.textfield.aspx), with the following remark on the MSDN page…
 7
 8“Every [TaxonomyField](http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.taxonomyfield.aspx) object contains a related hidden text field that contains a string representation of the taxonomy field value. The hidden text field is identified by the GUID returned by this property.”
 9
10…so as well as the defining the Taxonomy Field we also need to define something to store the string representation.

1: public override void FeatureActivated(SPFeatureReceiverProperties properties)

1 2: {

3: SPSite site = properties.Feature.Parent as SPSite;

1 4:    Guid fieldId = new Guid("{087C759A-7BD2-4B66-9CF5-277A3399636D}");

5: if (site.RootWeb.Fields.Contains(fieldId))

1 6:    {

7: TaxonomySession session = new TaxonomySession(site);

1 8:  

9: if (session.TermStores.Count != 0)

1 10:        {

11: var termStore = session.TermStores["Managed Metadata Service"];

1 12:            var group = termStore.Groups\["_Test Store_"\];

13: var termSet = group.TermSets["Categories"];

1 14:            TaxonomyField field = site.RootWeb.Fields\[fieldId\] as TaxonomyField;

15: // Connect to MMS

1 16:            field.SspId = termSet.TermStore.Id;

17: field.TermSetId = termSet.Id;

1 18:            field.TargetTemplate = string.Empty;

19: field.AnchorId = Guid.Empty;

1 20:            field.TextField = new Guid("{437B0ED2-A31B-47F7-8C69-6B9DE2C4A4F6}");

21: field.Update(true);

1 22:        }

23: }

1 24: }
2```
3
4Line 20 is the important one, this is the code that wires the Note field created declaratively to the TaxonomyField.