Products module: Adding and removing meta fields

Products module: Adding and removing meta fields

March 21, 2009 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

[This post is part of the developer's manual preview published on this blog. You can find temporary TOC here.]

One of the most useful features of Generic Content module is the ability to simply declare additional data for the content item. Generic Content based modules inherit this feature as well. Before we dig into the specifics, let us quickly examine the possible usages of this feature.


Generic Content module is a module for managing content items. We can specify arbitrarily number of additional data (in Sitefinity called meta data) to each content. So, for example, the base implementation of Generic Content module has following data in addition to the content itself:
  • Name
  • Description
  • Author
  • Category
If we take a look at the News module (News module is based on Generic Content module) we will see that in that module we use completely different data:
  • Title
  • Summary
  • Author
  • Source
  • Publication Date
  • Expiration Date
  • Thumbnail
  • Category
In conclusion, we see that metadata actually defines our data model and thus it allows us to use same base implementation of Generic Content module for various purposes.


Three points of meta data implementation

When working with meta fields, we are always working with three different aspects:
  • Declaration - letting Sitefinity know about the metafields we wish to use
  • Admin user interface - providing controls for managing values of meta data on the backend
  • Public user interface - providing controls for displaying the values of meta data on the website

Persistence and several other things, however, are automatically handled by Sitefinity making this whole process a rather simple task.


Meta data declaration

The very first thing we have to do if we are to add new meta field is to declare it. All Generic Content meta fields are located in web.config file in the telerik/cmsEngine/metaFields section. For each Generic Content provider we have to declare meta fields. While this may seem as an unnecessary requirement if we have multiple providers for the same module, it is actually a rather powerful feature which allows us to have one module working with different data based on the provider.


Let us now examine the meta field declaration for the sample products module:
<add key="Products.Name" valueType="ShortText" visible="True" searchable="True" sortable="True" defaultValue="Name this content" mandatory="True" /> 
<add key="Products.Price" valueType="ShortText" visible="True" searchable="True" sortable="True" defaultValue="" /> 
<add key="Products.SKU" valueType="ShortText" visible="True" searchable="True" sortable="True" defaultValue="" /> 
<add key="Products.Weight" valueType="ShortText" visible="True" searchable="True" sortable="True" defaultValue="" /> 
<add key="Products.Category" valueType="ShortText" visible="True" searchable="True" sortable="True" defaultValue="" /> 
We can see that we have defined five meta fields, while the actual content will be the description of the product. So, for each product we will define its name, its price, its stock keeping unit, it’s weight and category.


The actual syntax of the meta field element can be described as following:

  • key
    Unique name of the meta field on the provider. The key is used for identifying meta field.
  • valueType
    Defines the type of value that will be stored in this metafield. The supported value types are:
    • ShortText
    • LongText
    • DateTime
    • Integer
    • FloatingPoint
    • Boolean
    • Guid
    • Binary
  • visible
    Defines whether meta field should be displayed in the user interface (e.g. sometimes we just need to persist certain value with the content that will be used exclusively through our code and should not be visible to end users).
  • searchable
    If we mark field searchable, end users will be able to search the content by value of this meta field. Searchable fields will be visible in the search drop down list.
  • sortable
    End users will be able to sort content items based on meta fields which are marked sortable.
  • defaultValue
    Default value of the meta field, which end user should see if no other value has been set.
  • mandatory
    If true, user must enter the value for this field
Once we have declared all the meta fields we need for our content items, we can move to the second point - providing user interface on the administrative side.


Admin user interface

Determining where exactly we should provide user interface for our meta fields depends on the implementation of the module, however, if meta field is declared as visible - we will most probably have to provide user interface for:
  • Inserting new content and
  • Editing content
In the next article of this series, we will also demonstrate how to display meta fields in the RadGrid which displays list of all items.


The process for defining meta fields user interface is identical for both inserting and editing content, so we will explain it only on the sample for inserting content.


In the template for ProductEditView view, we should locate the ContentMetaFields control and inside of it we need to place a control which implements ITextControl interface (TextBox, DropDownList…) and make sure that the ID of the control is same as the key of the metafield for which the control is being defined. In practice, this means that our code would look something like this:
<sf:ContentMetaFields id="MetaFields" runat="server"
     <asp:TextBox ID="Price" runat="server"></asp:TextBox> 
Sitefinity knows based on the configuration that a meta field with key Price has been declared and will attempt to find a control with same id. If it finds it successfully, it will take the value of its Text property (mandated by ITextControl property) and persist it.

*** BEGIN NOTE ***

While it may seem at first that this approach would serve only very simple scenarios, the fact that Sitefinity works against ITextControl interface allows for very complex meta field editors. For example, one could create a user control, wrap RadEditor inside of it and implement ITextControl interface on it - thus providing rich text editor as a meta field editor.

*** END NOTE ***

Public user interface

Sitefinity automates many of the meta data related functions as we have already seen in the previous paragraphs. One of these simplifications is also automated data binding on the public side through ContentView control.


All that one needs to do - in order to display the value of meta field - is to place ITextControl (e.g. Label) in the ContentView template and set the ID of the ITextControl same as the key of the meta field for which we wish to display the value.


For example, we could place following markup to display the weight of the product:

<asp:Label ID="Weight" runat="server"></asp:Label> 
Similarly to the way Sitefinity is able to automatically find and save the value of metafield, it will also be able to automatically find the control (by matching its ID and the key of metafield) and thus display the value of metafield with no coding required on our side.

The Progress Guys

View all posts from The Progress Guys on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments are disabled in preview mode.
Latest Stories in
Your Inbox
More From Progress
5 Questions Automakers Should Ask to Improve Asset Uptime
Download Whitepaper
2018 Gartner Magic Quadrant Web Content Management (WCM)
Download Whitepaper
What Serverless Means for Enterprise Apps
Watch Webinar