+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

Products module: Adding and removing meta fields

by Ivan Osmak

[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"
   <ItemTemplate> 
   … 
     <asp:TextBox ID="Price" runat="server"></asp:TextBox> 
   … 
   </ItemTemplate> 
</sf:ContentMetaFields> 
 
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.

3 comments

Leave a comment
  1. Kazi Apr 09, 2009
    How does one do this in 3.6?
  2. Georgi Apr 24, 2009
    Hi Kazi,

    You should first map the necessary view to an External Template. They you could change this template, just like in 3.5 and below
  3. Kazi Nov 02, 2009
    Thanks Georgi...

    External Templates in 3.6 are not available in the base code. They need to be downloaded from your Sitefinity account. I did not know this, and kept on looking for the templates. :)

    Thanks!

    Leave a comment