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];
try
{
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;
taxField.Update();
web.Fields.Add(taxField);
web.Update();
newsList.Fields.Add(taxField);
//newsListView.ViewFields.Add(t);
}
}
catch (Exception ex)
{
}
But the last line
web.Fields.Add(taxField)
throws an error
Key cannot be null.
Any idea what this could be?