To add these to our custom list definition we use an element file similar to the one below (note the List Template Id should match the Type attribute of our list definition).
As a final step we can optionally create a list instance so that when we deploy our solution lists are created automatically.
I’ve based the code on Wictor’s excellent example with a couple of minor updates.
Instead of hardcoding the name of the term store I’m getting the default keyword store associated with the site (this means we do not have to hardcode the name of the managed metadata service but you should check this works in all your environments).
section to the field and hardcode the various Id’s that define a term set (required if you are using a sandboxed solution), or configure these in code.
I’m taking the code approach here as it is the only way to ensure the field will work across multiple environments.
At this point we have a robust way of deploying a standalone site column which is useful in certain situations but in many situations we need to provision content types, list definitions and list instances which is when things get tricky…
As explained in the previous post Issues provisioning Share Point 2010 Managed Metadata fields – to ensure the metadata field automatically appears in the search refinement panel as it does for all other managed metadata fields we need to add the Tax Catch All columns to our list definition and wire up two taxonomy event receivers.
This and the following step is often missed out in other articles but cause problems if you try to create custom list definitions that include the managed metadata column. The problem stems from the fact that Share Point is looking for an associated note (i.e.Sidenote: you want to remove the /Lists/ prefix from the URL when creating document libraries to avoid breaking the default column values feature.You will also want to add in the file dialog view as it goes missing when using the Visual Studio 2010 tools.I’ve also created an example project that I’ve included at the end of the article if you want to see this working for yourself.Create a managed metadata field The first step is to create a site column using the Field element with a Type of either Taxonomy Field Type (allowing a single selection) or Type Taxonomy Field Type Multi (allowing multiple selections if you also set Mult=”True”).