This week I was asked to create some content types with Managed Metadata field in it. This of course is not hard to do if you do it from the UI, but we wanted to have a nice solution to deploy.
After searching the internet I found several posts how to do this, but none had a complete solution so here it is.
First of all you need to have a field that looks like this:
The GUID under Property Value needs to reference to an existing field as shown below and should be added to a content type as well.
After this you can create the content type as you normally do. But when you add the Taxonomy Field you need to add 2 additional fields to your content type to get it to work.
These fields are:
Now we have a content type with a Managed Metadata field but it does not reference to a termset. For this we have to create a feature receiver to bind the Taxonomy field to the correct termset. For this we can use the following code
This code uses two extra classes to make it more usable.
The first is an extension on Group so we can search for a name within the termstore groups
The second one is to search for a term in a Termset so we can use a term as the starting point instead of a Termset.
The resources I have used fort his are:
Correctly Provisioning Managed Metadata (Taxonomy) Fields through CAML in SharePoint 2010 http://www.wictorwilen.se/Post/How-to-provision-SharePoint-2010-Managed-Metadata-columns.aspx http://www.sharepointconfig.com/2011/03/the-complete-guide-to-provisioning-sharepoint-2010-managed-metadata-fields/ http://msdn.microsoft.com/en-us/library/aa979575(v=office.15).aspx