To recap on the previous post we are aiming to avoid the following problems when creating SharePoint 2010 taxonomy fields through features:

If you create a list definition that uses this site column you get the error: Failed to get value of the “{0}” column from the “Managed Metadata” field type control. See details in log. Exception message: Invalid field name. {00000000-0000-0000-0000-000000000000}.

The managed metadata column does not appear in the search refinement panel as per columns created through the UI.

If the managed metadata features are not activated in the site the field will not work (and no obvious errors will be displayed).

We found the first problem was due to a missing note field when creating a list definition that used the site column. The second problem was due to the missing TaxCatchAll and TaxCatchAllLabel columns and missing event receivers on the list definition. The last issue can be easily avoided by adding a feature dependency to ensure the required feature is activated before we deploy our custom site column. With this information we can use the following steps to deploy managed metadata fields that avoid these issues. 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.

Configure the managed metadata column to reference an existing term set

Provision a hidden field of type Note

Configure the managed metadata column to use the hidden Note field

Ensure that the TaxonomyFieldAdded feature is activated on the site collection

Create a list definition that uses a managed metadata field

Create a content type that uses the managed metadata fields and the TaxCatchAll columns

Create a list definition that uses the content type and includes all metadata and TaxCatchAll columns

Attach the TaxonomyItemSynchronousAddedEventReceiver and TaxonomyItemUpdatingEventReceiver event receivers to the list definition

Create a list instance to provision instances of the list definition

Create a managed metadata field

1. Provision a managed metadata field

The first step is to create a site column using the Field element with a Type of either TaxonomyFieldType (allowing a single selection) or Type TaxonomyFieldTypeMulti (allowing multiple selections if you also set Mult=”True”).

<FieldID="{F88D530B-705F-488A-9069-FAF5C79B61F7}"

Type="TaxonomyFieldType"

DisplayName="Regional Office"

ShowField="Term1033"

EnforceUniqueValues="FALSE"

Group="Custom Columns"

StaticName="RegionalOffice"

Name="RegionalOffice">

</Field>

Configure the managed metadata column to reference an existing term set

To link the field to a term set so users can select terms you can either add a <Customization/> 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.

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). I’ve also added in some additional error handling so we get informative messages if the metadata service and/or term set does not exist.

thrownew ArgumentOutOfRangeException("name", name, "Could not find the term set");

}

}

Note that if we are only creating a site column (or even a content type) this should work without completing the following steps. When we add either the site column or content type to a list the managed metadata field works and shows up in the search refinement panel (at least it did in my testing). This is due to the fact that SharePoint automatically wires some extra bits (shown below) when the field is added. At least in my testing when we create a list definition this isn’t all wired up correctly and the following steps were required.

Provision a hidden field of type Note

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 specific error message you get is:

Error – Failed to get value of the “{0}” column from the “Managed Metadata” field type control. See details in log. Exception message: Invalid field name. {00000000-0000-0000-0000-000000000000}

The problem stems from the fact that SharePoint is looking for an associated note (i.e. text) field that is required for the metadata column to work. The first taxonomy field we created actually only stores a lookup value to a list within the current site collection and it is the note field that actually stores the names and Id’s of each selected term. So to create this note field we can add a field similar to the following:

<FieldType="Note"

DisplayName="Regional Office_0"

StaticName="RegionalOfficeTaxHTField0"

Name="RegionalOfficeTaxHTField0"

ID="{9EDAB26E-CC44-4027-AB05-CB44EA3A6F72}"

ShowInViewForms="FALSE"

Required="FALSE"

Hidden="TRUE"

CanToggleHidden="TRUE"

RowOrdinal="0"/>

Note: SharePoint uses the suffix _0 appended to the display name of the field and TaxHTField0 after the static name so I’m following those conventions.

Configure the managed metadata column to use the hidden Note field

To link the two together we can update the field we created in step one to include a customization section that points to the note field as shown below:

Note the value within the TextField property is the ID of the note field we created above.

Ensure that the TaxonomyFieldAdded feature is activated on the site collection

This is another step that isn’t always mentioned but if this feature is not present then you are presented with a greyed out metadata selection when adding or editing items (note this also happens if the field is not connected to a term set). You won’t get any obvious error messages and the feature is hidden so you won’t see it in the site features list either.

The TaxonomyFieldAdded feature is not activated on sites created using the blank site template, or sites created using custom site definitions so a good idea is to include a feature activation dependency that ensures this is activated before we attempt to provision our field. This is easy to do using the Visual Studio 2010 feature editor – just expand the Feature Activation Dependencies section at the bottom and add the following information:

Title: TaxonomyFieldAdded

Feature ID: 73ef14b1-13a9-416b-a9b5-ececa2b0604c

Description: Register taxonomy site wide field added event receiver

Assuming the feature that contains the site columns is site scoped this feature will activate automatically if it isn’t already activated.

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…

Create a list definition that uses a managed metadata field

As explained in the previous post Issues provisioning SharePoint 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 TaxCatchAll columns to our list definition and wire up two taxonomy event receivers.

Create a content type that uses the managed metadata field

I find the easiest way to ensure all the columns are added to the list definition is to add them to a content type first. This content type should contain the managed metadata/taxonomy field, the note field and the TaxCatchAll and TaxCatchAllLabel fields as shown below.

Create a list definition that uses the content type and includes all metadata and TaxCatchAll columns

We can do this first step using the Visual Studio 2010 tools to create a list definition from the content type we created earlier. As we added the TaxCatchAll columns to our content type these will automatically be added to our list definition.

Attach the TaxonomyItemSynchronousAddedEventReceiver and TaxonomyItemUpdatingEventReceiver event receivers to the list definition

If we inspect a list that contains a managed metadata field created through the UI it shows two event receivers. These are the TaxonomyItemSynchronousAddedEventReceiver for the ItemAdding event and the TaxonomyItemUpdatingEventReceiver for the ItemUpdating event. To add these to our custom list definition we use an element file similar to the one below (note the ListTemplateId should match the Type attribute of our list definition).

Create a list instance to provision instances of the list definition

As a final step we can optionally create a list instance so that when we deploy our solution lists are created automatically. This should be done as a separate web scoped feature. All going well we will now have a list that contains a correctly functioning managed metadata field which automatically gets extracted during search processing and displayed in the search refinement panel as shown below.

Example Project

To run the project you will need a machine with Visual Studio 2010, SharePoint Server 2010 and and managed metadata service with a Group named ‘Global’ and a Term Set named ‘Office Locations’ (or you can wire this up to a term set of you choice by changing the code in the feature receiver).

I’m sure there are other rough edges that I haven’t covered yet so let me know if you have come across any other odd behaviour when provisioning managed metadata site columns in the comments below.

Hi Ari
Just been having a conversation with Anders Rask and he says you should include DisplayOnUpgrade which is set declaratively in field manifest and also set TextField in feature receiver as he has had experience of the field being corrupted on upgrade.

Good article! Will definetely check out the TaxCatchAll and TaxCatchAllLabel fields, as i dont add those at the moment.

As Nigel say, use DisplaceOnUpgrade=”true” on all taxonomy fields. This tells SharePoint that if the field definition already exists we want to overwrite it with the new declarative xml (eg. the issue i mentioned on your Part 1 blog post, where i noticed multiple orphaned hidden notes fields).

Setting TextField in the feature receiver means you got everything in one place, but since you know your ID already i guess you could opt for adding it declaratively as well (as you do)

@Nigel, @Anders I’ve done some tests and if you use the method above to set the TextField as you provision the field these will upgrade without any problems. As you mention if you do not include the TextField during initial provisioning then you run into all sorts of problems (errors and corruption on upgrade in some case). In this case you would need to use the DisplaceOnUpgrade=”true” to upgrade them to use the correct TextField but if you use the method above this isn’t necessary.

I have a question, I published an infopath form published as an administrator approved form, the i activate de form on a collection site then i added my form on a document library but how do i need to do that use those fields as managed properties beacuse i can’t see de ows_fields

Hi, have you come across when the feature is deactivated, the notes fields still remain in sharepoint, this makes feature redepoyment/reactivation difficult? i have included the line below in feature deactivation but still cannot get rid of it…

Thank you for the great guide. I followed your post and successfully created a managed metadata site column. I was able to use the site column in a list that I created and everything went smoothly; however, when I went to deactivate, retract, and remove the feature and the solution, I expected to see the site column disappear from my site collection. The site column is still stubbornly there!

My question: Do we need to write additonal code in the FeatureUninstalling method to have the site column completely removed? (BTW, I had also created two more ‘generic’ site columns -single line text and choice – within the same feature and they were removed properly upon feature deactivation). Looking forward to hearing form you. Thank you again!

Hi. Great Article. I’ve following it to a “T” and it worked for a while. After a few deployments and retractions, etc I’m getting the original error again, but instead of a GUID of {00000…}, it’s displaying the GUID for the NoteField that I defined. Looking at this field using SharePoint Manager, I see that there are three fields with appended “_x”, like this “MyField_0″ and “MyField_1.” Someone commented earlier that these could not be deleted, and I am finding that is true as well. I could really use some help. Thank you!

@Ben L and @MSS – if you make updates to a column (content type or list) through the UI or the Object Model (as we do above) it won’t be removed when you deactivate the feature unless you write code in the FeatureDeactivated event to do this. You might also want to try the DisplaceOnUpgrade parameter in the taxonomy field definition as Anders mentioned to avoid the update issues if you are having problems with this. Personally I leave the fields in the site on feature deactivation so it does not break things if users have created lists that use these fields.

@JakeJ I’ve deployed and reactivated dozens of times and haven’t been able to reproduce that issue. If you have a set of steps that can be used to replicate the issue please let me know and I’ll look into it further. The only way I’m aware of the duplicate note field problem occurring is when the TextField is not set correctly and/or you use the Update(true) flag when updating the field (which I’m not doing). You might also want to try Anders’ suggestion and use the DisplaceOnUpgrade flag or if it is a development environment rebuild the site collection so you have don’t have broken fields lying around.

Hi Ari. Thanks for responding. I was setting the field.Update(true) flag when updating the field. As I understand, that propagates changes to any lists that implement that field. In my case, this field is in a page content type. I receive the error when creating a new page. I’ll change that flag. I did see Ander’s suggestion after I posted my comment. I’ve added the DisplayOnUpgrade flag. Unfortunately, this is a production system, so I can’t rebuild, but I ran some code to hook up the correct MM field and MM note field together. After that, I manually removed the MM field from the content type and re-added it back. I was able to create a page with the MM field without the error. Thanks again!

Thanks for this article…I have a question regarding binding a field to a termset in the store.

In your FeatureActivated code example, you show only how to assign a termset to the field as…

field.TermSetId = termSet.Id;

I have through the UI been able to bind a managed metadata field to a TermCollection…ie. A group of terms beneath the TermSet. I can’t figure out how to do that in code…I tried assigning the id of the root term of the term collection to the field.TermSetId property but it doesn’t seem to take in my testing.

If you look at the section at the bottom of the article called “Example Project”, what I’m trying to do is to bind directly to ‘Pacific’, which is a TermCollection, rather than to ‘Office Locations’ which is the TermSet.

I’m able to do this through the end-user interface. But can’t get it to stick when assigning in code.

Thanks for this great article Ari. Been banging my head against a wall for ages around this.

One thing I had to do to get TaxonomyCatchAll and TaxonomyCatchAllLabel populated with data (and thus get the managed / crawled properties to auto-create) which wasn’t mentioned in the article but was present in your demo project is as follows :

When you use VS 2010 to create a list def based on your content type with TaxonomyCatchAll and TaxonomyCatchAllLabel, the schema.xml contains references to these fields in (this is OK) but also contains references to them in section. It refers to them as lookups instead of taxonomy. Until I removed these as per your demo project those 2 fields wouldn’t get populated.

[…] SharePoint 2010 allows you to centrally manage metadata and keywords. In a previous post I covered how to provision a SharePoint 2010 managed metadata field and add this to a content type and a list definition. In this post I will cover how to add these […]

I have been trying to use the process stated above to no avail. My situation is as follows:
I am trying to define 5 content types in one list definition all of them using one or more managed metadata fields. When submitting a new item using the first content type everything works fine. As soon as I try a second content type the form produces the error: Failed to get value of the “{0}” column from the “Managed Metadata” field type control. See details in log. Exception message: Invalid field name.

Note that if you use the AutoSPInstaller script to install your SharePoint environment you might have an issue with the DefaultKeywordsTermStore property of the TaxonomySession. This issue is documented at http://autospinstaller.codeplex.com/workitem/17200 and offers an alternative PowerShell command that can be used to ensure this property is set correctly.

Ari,
Would you do anything differently if you wanted to add the Metadata columns to a Publishing content type? In this case, I’m building out a content type that inherits from Page. I’m using a content type binding to bind the content types to a Pages library.

Thanks Ari, I discovered that post after posting my question – thanks so much for putting both of these together.

After provisioning the columns how you describe in the post, the issue I’m having is that only one of my content types appears in the refinement panel. I posted a question about this on stackexchange if you’d like to see the details about the content types. Thank you!

One question, I’ve gotten this to work flawlessly if I activate the features manually, but if I use the exact same feature and try to activate it automatically as part of a site definition I get a TargetInvocationException in the ConnectTaxonomyField method. Specifically, on the line

TaxonomyField field = web.Fields[fieldId] as TaxonomyField; (my feature is scoped to web instead of site)

I can use the exact same site definition without the feature in the onet.xml, and everything works fine when I activate the feature manually.

Hello, thank you for this tutorial. I’m brand new to managed metadata fields and this is perfect. I’ve followed this example but when I deploy, i keep getting the error “Error occurred in deployment step ‘Activate Features': Operation is not valid due to the current state of the object.” I’ve researched this over and over, and I’m stumped. Do you happen to know what might be causing this? Thanks, Shaun

@Ari Bakker Thanks for the reply Ari. I found a mistake in the EvenReceiver code I did and one other mistake in the C# code. A co-worker of mine got his to work with those corrections so I believe that should do it. I’ll post again once I’m sure.

I am migrating a 2010 site collection to a new site collection and facing a weird problem. Migration went well. Then I created some Managed Metadata columns and content types and assigned it to a document library. I could upload the document and fill the MM columns too but same how TaxCathall column is empty. Is there anything I have to do to get it working? Any help would be really appreciated.

I would like a little help regarding event recievers, actually i have a document respository with custom folder content types and document content types and both these document types have managed metadata columns. What i want is when a user creates a new folder it should first check whether that folder exists or not. If it exists then update that folder with the new values and if not then it should create that folder.

Can you suggest some way or any links where i can get some info and approach on how to procedd

@Sourabh the first question I would ask is do you really need folders or can you replace these with a metadata column? If you can I’d suggest avoiding folders and using metadata to classify documents instead. If you have a good reason to use folders and have a valid business requirement for building the type of customisation you mention then I’d suggest investigating whether a custom new form template (i.e. the create folder page) would work. The article Creating Custom Form Templates at http://msdn.microsoft.com/en-us/library/aa544142.aspx has information on this.

@Ari Thanks for the Great Article. Do you know if it possible to do the above and deploy columns and content types in a Web scoped feature? ie: to a child (web)site of the site collection. I really want to evenly distribute my elements across the intranet sites rather that dump them all in the root. I have tried but cant make it work without scope the feature to site. (I’m using SP 2013)

After deploying your example code without error I progressed to make my own column. I generated some GUIDs from an internet site to use for my newTaxonomyFieldType and Note field elements. After several days of vague errors and feature activation fails I replaced the generated GUIDs with GUIDs from an SP 2013 site (different server). And now my new column deploys without failure.

[…] However I could not find why this was happening. I was following a blog detailing how to Provisioning SharePoint 2010 Managed Metadata fields and could not see where I was straying from his example. After hours of hunting for the […]

I have been struggling with ‘greyed metadata’ field for few hours now. My problem was that metadata column didn’t seem to connect to term set. I checked all the things on this post (and many others) but couldn’t figure out the why… UNTIL!

I realised that I had all the components (field, content type, list definition and list intance) in the same feature. Event receiver fired of course, but it didn’t update the column that was already in the list instance (since it had been created already, at least I think so).

Solution was to separate field and content type in one feature (with event receiver) and list instance to another.