Persistence and several other things, however, are automatically handled by Sitefinity making this whole process a rather simple task.
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:
Unique name of the meta field on the provider. The key is used for identifying meta field.
Defines the type of value that will be stored in this metafield. The supported value types are:
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).
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.
End users will be able to sort content items based on meta fields which are marked sortable.
Default value of the meta field, which end user should see if no other value has been set.
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> |
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.