Creating classification fields

To learn more about the different steps to create a custom field, read the Creating custom fields article.

Here is a list of the things that are specific for the classification field:

  • Field CLR type: The classification field does not use this parameter.
  • User friendly data type: UserFriendlyDataType.Classification
  • DB type: The classification field does not use this parameter.
  • Field type: typeof(HierarchicalTaxonomyField).FullName or typeof(FlatTaxonomyField).FullName

In addition, when creating a classification field you must perform the following:

  • Get an instance of the taxonomy that is used by the field.
    To do this, you must call the GetTaxonomy method of the TaxonomyManager instance and pass the ID of the taxonomy as an argument.
  • Skip Step 7, the creating of a database mapping.
  • In step 8, substep 2, while creating the field definition set the following additional properties:
    • TaxonomyId
      Gets or sets the ID of the taxonomy that is used by the field.
    • AllowMultipleSelection
      Specifies whether the item will by classified by single taxon or by multiple taxa. Only the classification field requires this property. Set this property to true.
  • In step 8, substep 2, while creating the field definition set the FieldType property depending on the type of the taxonomy (flat or hierarchical).
  • Skip step 13, substep 4, where the database mappings are set.
  • In step 13, substep 5, set the following additional properties of the MetaField instance:
    • TaxonomyId
      Gets or sets the taxonomy used by field. Only the classification field requires this property.
    • TaxonomyProvider
      Gets or sets the provider of the taxonomy used by field. Only the classification field requires this property.
    • IsSingleTaxon
      Specifies whether the item will by classified by single taxon or by multiple taxa. Only the classification field requires this property.
  • In step 13, substep 6 you must also add the following meta attribute:
    • ControlTag
      The value of the attribute must be a taxonomy field control template. To get the value for the attribute, call the GetTaxonomyFieldControlTemplate static method of the TaxonomyManager class and pass name of the field and the instance of the taxonomy.

Here is a code example:

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);
}

Next steps

+1-888-365-2779
sales@sitefinity.com

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK