I am trying to create a TaxonomyField programmatically and add it to a list. This is my code

TaxonomySession session = new TaxonomySession(site);
TermStore termStore = session.DefaultKeywordsTermStore;

Group termGroup = termStore.Groups[Const.TAXONOMY_MA_TERMGROUP_NAME];
string termSetX = "myTermSet";

TermSet termSet = termGroup.TermSets[termSetX];

    if (web.Fields.TryGetFieldByStaticName(t) == null)
        TaxonomyField taxField = web.Fields.CreateNewField("TaxonomyFieldType", t) as TaxonomyField;

        taxField.SspId = termSet.TermStore.Id;
        taxField.Group = termGroup.Name;
        taxField.TermSetId = termSet.Id;
        taxField.AllowMultipleValues = true;
        taxField.TargetTemplate = string.Empty;
        taxField.AnchorId = Guid.Empty;


catch (Exception ex)

But the last line


throws an error

Key cannot be null.

Any idea what this could be?

  • Have you checked that your TermStore and TermSet ID exists? You should open a taxonomySession and request those values directly from there. – Stefan Bauer Nov 25 '15 at 9:10
  • I updated my code and i am 100% sure that TermSet and TermGroup exist. – BOG Nov 25 '15 at 9:13
  • Can you try to change the DefaultKeywordsTermStore to DefaultSiteCollectionTermStore. The one you are using is just for Enterprise Keywords but not for all other term sets. – Stefan Bauer Nov 25 '15 at 9:17
  • This is the correct TermStore. It is filled centrally with Masterdata. I am using it already on several custom application pages where it works fine. I am also able to create manually a list and add a managed metadata column and bind it to this TermStore. But i am not able to programmatically add a TaxonomyField to a list. – BOG Nov 25 '15 at 9:20
  • Had a similar issues a couple of times with the default key word term store. I currently always try to find a matching Term Set in all Term stores. – Stefan Bauer Nov 25 '15 at 9:21
  1. I think web.Update(); is not needed. But probably not related to your problem.
  2. Here's how I've already done it. Note I update field properties only after adding it to the site columns collection (web.Fields.Add called before field.Update()):
if (!web.AvailableFields.ContainsField(fieldInternalName))  
   field = web.Fields.CreateNewField("TaxonomyFieldType", fieldInternalName) as TaxonomyField;  

field = web.Fields.GetFieldByInternalName(fieldInternalName) as TaxonomyField;  
field.Title = "Field display Name";  
field.Description = "Description";  
field.Required = true;  
field.Group = "CustomGroup";  
field.AllowMultipleValues = true;  
field.IsPathRendered = true;  

TaxonomySession session = new TaxonomySession(web.Site);  

// get the default metadata service application      
TermStore termStore = session.DefaultSiteCollectionTermStore;  
Group group = null;  
if (string.IsNullOrEmpty(termGroupName))  
   group = termStore.GetSiteCollectionGroup(web.Site);  
   group = termStore.Groups.GetByName(termGroupName);  
TermSet termSet = group.TermSets.GetByName(termSetName);  

field.SspId = termSet.TermStore.Id;  
field.TermSetId = termSet.Id;  
field.TargetTemplate = string.Empty;  
field.AnchorId = Guid.Empty;  
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.