In addition, when creating a classification field you must perform the following:
public static void CreateClassificationField(Guid productTypeId, string fieldName, string fieldLabel, Guid taxonomyId)
{
CatalogManager catalogManager = CatalogManager.GetManager();
TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
ProductType productType = catalogManager.GetProductType(productTypeId);
if (productType == null)
{
return; //The product type does not exist.
}
ITaxonomy taxonomy = taxonomyManager.GetTaxonomy(taxonomyId);
if (taxonomy == null)
{
return; //Taxonomy does not exist.
}
Type productClrType = TypeResolutionService.ResolveType(productType.ClrType);
UserFriendlyDataType userFriendlyDataType = UserFriendlyDataType.Classification;
WcfField wcfField = new WcfField()
{
Name = fieldName,
ContentType = productClrType.FullName,
FieldTypeKey = userFriendlyDataType.ToString(),
IsCustom = true,
//Field definition
Definition = new WcfFieldDefinition()
{
Title = fieldLabel,
FieldName = fieldName,
FieldType = taxonomy is HierarchicalTaxonomy ? typeof(HierarchicalTaxonField).FullName : typeof(FlatTaxonField).FullName,
TaxonomyId = taxonomyId.ToString(),
AllowMultipleSelection = true,
}
};
CustomFieldsContext.Validate(wcfField, productClrType);
var typeContext = new CustomFieldsContext(productClrType.FullName);
typeContext.SaveFieldDefinition(wcfField, productClrType.Name, null);
typeContext.SaveChanges();
using (MetadataManager metadataManager = MetadataManager.GetManager())
{
MetaType metaType = metadataManager.GetMetaType(productClrType);
MetaField metaField = metadataManager.CreateMetafield(fieldName);
metaField.Title = fieldLabel;
metaField.IsSingleTaxon = false;
metaField.TaxonomyId = taxonomyId;
metaField.TaxonomyProvider = ((DataProviderBase)taxonomy.Provider).Name;
metaField.MetaAttributes.Add(new MetaFieldAttribute { Name = "UserFriendlyDataType", Value = userFriendlyDataType.ToString() });
metaField.MetaAttributes.Add(new MetaFieldAttribute { Name = "IsCommonProperty", Value = "true" });
string fieldTag = TaxonomyManager.GetTaxonomyFieldControlTemplate(wcfField.Name, taxonomy);
if (fieldTag != null)
{
metaField.MetaAttributes.Add(new MetaFieldAttribute { Name = "ControlTag", Value = fieldTag });
}
metaType.Fields.Add(metaField);
metadataManager.SaveChanges();
}
//SystemManager.RestartApplication(false);
}