Creating long text 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 long text field:

  • Field CLR type: typeof(string).FullName
  • User friendly data type: UserFriendlyDataType.LongText
  • DB type: “CLOB”
  • Field type: typeof(HtmlField).FullName

When creating a long text field you must perform the following in addition:

  • In step 13, substep 6, you must also add the following meta attribute:
    • ControlTag
      The value of the attribute must be the following:
      string.Format("<sitefinity:HtmlField runat=\"server\" DisplayMode=\"Read\" Value='<%# Eval(\"{0}\")%>' />", wcfField.Name)

Here is a code example:

public static void CreateLongTextField(Guid productTypeId, string fieldName, string fieldLabel)
{
    CatalogManager catalogManager = CatalogManager.GetManager();
 
    ProductType productType = catalogManager.GetProductType(productTypeId);
    if (productType == null)
    {
        return; //The product type does not exist.
    }
 
    Type productClrType = TypeResolutionService.ResolveType(productType.ClrType);
    string fieldClrType = typeof(string).FullName;
    UserFriendlyDataType userFriendlyDataType = UserFriendlyDataType.LongText;
 
    var wcfField = new WcfField()
    {
        Name = fieldName,
        ContentType = productClrType.FullName,
        FieldTypeKey = userFriendlyDataType.ToString(),
        IsCustom = true,
 
        //Database mapping
        DatabaseMapping = new WcfDatabaseMapping()
        {
            ClrType = fieldClrType,
            ColumnName = fieldName.ToLower(),
            Nullable = true,
            DbType = "CLOB",
        },
 
 
        //Field definition
        Definition = new WcfFieldDefinition()
        {
            Title = fieldName,
            FieldName = fieldName,
            FieldType = typeof(HtmlField).FullName,
        }
    };
 
    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);
 
        CustomFieldsContext.SetFieldDatabaseMappings(metaField, metaType, userFriendlyDataType, wcfField, metadataManager);
 
        metaField.Title = fieldLabel;
 
        metaField.MetaAttributes.Add(new MetaFieldAttribute { Name = "UserFriendlyDataType", Value = userFriendlyDataType.ToString() });
        metaField.MetaAttributes.Add(new MetaFieldAttribute { Name = "IsCommonProperty", Value = "true" });
        metaField.MetaAttributes.Add(new MetaFieldAttribute
        {
            Name = "ControlTag",
            Value = string.Format("<sitefinity:HtmlField runat=\"server\" DisplayMode=\"Read\" Value='<%# Eval(\"{0}\")%>' />", wcfField.Name)
        });
 
        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