Create custom fields for product types

This topic explains the common approach that is used to create fields.

The links above lead you to a topic for each field. In these topics you will find code examples for creating each of the field types and information about the specifics of the field.

Each field has the following specific values:

  • Field CLR type
  • User friendly data type
  • DB type
  • Field type

For the multiple choice and the yes/no fields you must also add a list of choices. For the classification fields you must specify the ID of the taxonomy.

To create a custom field, you must perform the following:

  1. Get the ecommerce manager.
    Get an instance of the EcommerceManager object.
  2. Get the specified product type.
    Get an instance of the specified product type that you want to create a custom field for. For more information, see For developers: Query product types.
  3. Resolve the product CLR type.
    To resolve the product CLR type, call the ResolveType static method of the TypeResolutionService. As an argument use the value of the ClrType property of the ProductType instance.

    NOTE: To use the ResolveType method to resolve the dynamic product type, you must make sure that you have instantiated the CatalogManager before that. In this example the CatalogManager is instantiated in order to get the product type.

  4. Define the field CLR type and the user friendly data type values.
    For example, for the short text field they should have the following values:
    • User friendly data type: Telerik.Sitefinity.Model.UserFriendlyDataType.ShortText
    • Field CLR type: System.String
  5. Create an instance of the WcfField class.
    This class wraps the information about the field, its database mapping and its definition.
  6. Set the properties of the WcfField instance.
    In this example the following properties are set:
    • Name
      Gets or sets the name of the field. The value must not contain spaces or any unsafe characters.
    • ContentType
      Gets or sets the type of the content the field belongs to.
    • FieldTypeKey
      Gets or sets the UserFriendlyDataType value for the field.
    • IsCustom
      Indicates whether the field is a custom field.
  7. Create a database mapping.
    To create a database mapping you must perform the following:
    1. Create an instance of the WcfDatabaseMapping class.
      This class wraps the information about the database mapping.
    2. Set the properties of the WcfDatabaseMapping instance.
      In these examples the following properties are set:
      • ClrType
        Gets or sets the CLR type of the field.
      • ColumnName
        Gets or sets the name of the column in the database table.
      • Nullable
        Indicates whether the database column is nullable.
      • DbType
        Gets or sets the type of the column.
    3. Set the database mapping.
      Set the DatabaseMapping property of the WcfField instance to the WcfDatabaseMapping instance.
  8. Create a field definition.
    To create a field definition you must perform the following:
    1. Create an instance of the WcfFieldDefinition class.
      This class wraps the information about the field definition. The definition specifies the way the field is represented in the UI.
    2. Set the properties of the WcfFieldDefintion instance.
      In these examples the following properties are set:
      • Title
        Gets or sets the display title of the field.
      • FieldName
        Gets or sets the name of the field. The value must not contain spaces or any unsafe characters.
      • FieldType
        Gets or sets the field control type that is used to represent the field in UI.
      • TaxonomyId
        Gets or sets the ID of the taxonomy used by the field.

        NOTE: When creating a field of types multiple choice or yes/no, you must set the value of the Choices property. For more information, see the respective code examples.

    3. Set the field definition.
      Set the Definition property of the WcfField instance to the WcfFieldDefinition instance.
  9. Validate the field.
    To validate the field, call the Validate static method of the CustomFieldsContext class and pass the WcfField instance and the product CLR type as arguments.
  10. Create a new custom fields context.
    Create a new instance of the CustomFieldsContext class. Pass the full name of the product CLR type to the constructor.
  11. Build a definition for the field.
    To build the field definition, call the SaveFieldDefinition of the CustomFieldsContext instance. Pass the WcfField instance, the name of the product CLR type and a builder as arguments. Because in this case you don't need a builder, pass null.
  12. Save the changes made by the context.
    To save the changes, you must call the SaveChanges method of the CustomFieldsContext instance.
  13. Create a meta field.
    To create a meta field, you must perform the following:
    1. Get the metadata manager.
      Get an instance of the MetadataManager object.
    2. Get the meta type for the specified product type.
      To get the meta type, call the GetMetaType method of the metadata manager and pass the product CLR type as an argument.
    3. Create a meta field.
      To create a new meta field, call the CreateMetaField method of the metadata manager. Pass the name of the field as an argument.
    4. Set the database mapping related properties of the field.
      To do this, call the SetFieldDatabaseMappings static method of the CustomFieldsContext class and pass the following arguments:
      • The MetaField instance as the metaField argument.
      • The MetaType instance as the metaType argument.
      • The userFriendlyDataType value as the mappingKey argument.
      • The WcfField instance as the wcfField argument.
      • The MetadataManager instance as the metadataManager argument.
    5. Set the properties of the meta field.
      • Title
        Gets or sets the title of the field.
      • 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.
    6. Add meta attributes.
      To add a meta attribute to the field use the MetaAttributes collection of the MetaField instance. You must add the following attributes:
      • UserFriendlyDataType
        The value of this attribute must be the same as the value of the FieldTypeKey property of the WcfField instance.
      • IsCommonProperty
        The value of this attribute must be “true”.
      • ControlTag
        The classification and long text fields will require the “ControlTag” meta attribute.
    7. Add the meta field to the meta type.
      Add the MetaField instance to the Fields collection of the MetaType instance.
  14. Save the changes to the metadata manager.
    To save the changes, call the SaveChanges method of the metadata manager.
  15. Restart the application.
    To restart the application from code, call the static RestartApplication method of the SystemManager class.

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?