Sitefinity CMS

Add Custom Meta Fields Send comments on this topic.
How-to > Modules > Built-in Modules > Generic Content Based Modules > Add Custom Meta Fields

Glossary Item Box

Working with Built-in modules often requires functionality which is not fully provided. In order to extend modules based on Generic Content, meta fields could be added. This topic shows how to add meta fields and extend the News module with PDF files as attachments.

 

Solution

All Generic Content based modules (Generic Content, News, Blogs, Images and Documents, Events) have the ability to define custom meta fields. The procedure includes the following steps:

  1. Add a meta field to the <metaFields> tag in the web.config. The PDFAttachement meta field will be defined for the News module;

  2. Add a control that will accept the URL of the meta field; 

  3. Add a hyperlink control that will display this URL (for the .pdf attachment) in SingleNews and/or NewsList control;

 

Defining a New Meta Field

Open the web.config file. In the <cmsEngine> section find the <metaFields> section. There are numerous meta fields that modules use by default. Add the following tag with the specified key:

web.config Copy Code
<cmsEngine>
 
...
 
<metaFields>
   
...
   
<add key="News.PDFAttachment" valueType="ShortText" visible="True" searchable="false" sortable="False" defaultValue=""></add>

This tells Sitefinity that there will be a PDFAttachement field (of type ShortText) stored in each news item. After that, some user interface must be available in the admin area so that users could enter the URL of the .pdf file for the news item.

 

Modifying Admin Templates

The next step is to modify the templates that Sitefinity uses for inserting new news items and editing existing ones. To do so, the following files should be accessed in Visual Studio:

  •  ~/Sitefinity/Admin/ControlTemplates/News/ControlPanelInsert.ascx
  •  ~/Sitefinity/Admin/ControlTemplates/News/ControlPanelEdit.ascx

 

In order to enable templates to save the meta field, the field must be added to the following container:

<sfGCn:ContentMetaFields ID="editMetaFields" runat="server"> 
   ...
</sfGCn:ContentMetaFields>

 

There are two options to create the markup for a meta field (for example, Author): 

  1. Just add TextBox control and set its ID to PDFAttachement - that is how Sitefinity knows that the value of this TextBox should be used for saving/displaying a PDFAttachement meta field. The drawback of this solution is that the user needs to know the exact URL of the attachment (probably, attachments would be uploaded through the File Manager).
  2. Another option is to create a custom control that would, for example, have a "Select document to attach button". When this button is clicked, a RadWindow could be opened and provide a File System TreeView. In this way, users can select the document they wish to attach. If you decide to use a custom control, make sure it implements the ITextControl interface - the Text property will be used as a value of the meta field.

 

Afterwards, everything should be set up on the administration side. Users have the option to attach a document to a news item (by setting the .pdf document URL). Later, they can edit or remove it. The last step is to display this link on the news public control.

 

Displaying Meta Field Values on Public Controls

There will be a modification of the templates for the public controls. For example, let's assume that the desired functionality is the PDF attachment link to appear only when user opens a news item. In that case, only the template for the SingleNews control needs to be modified. The following file should be opened in Visual Studio:

  •  ~/Sitefinity/ControlTemplates/SingleNews.ascx

 

This is similar to the changes made to the admin templates. A Hyperlink control must be added with the same  ID as the meta key (PDFAttachement) so that Sitefinity knows that this is the control to which the value of that meta field will be bound. However, Sitefinity will only bind the Text property, so NavigateUrl must also be set. Following is a code that shows how the Hyperlink control should be written:

Hyperlink Control Copy Code
<asp:HyperLink ID="PDFAttachment" runat="server" NavigateUrl='<% = this.Text %>'></asp:HyperLink>


Basically, this tells the control that the NavigateUrl property is the same as the Text property of that control, and the text property will be set by Sitefinity.

 

Actually, there are more options. For example, two meta fields could be added: attachment file title and attachment file URL. Another option is to make the meta field store the collection of files (delimited by semi-colon ";"). Still, the solution provided is the most basic and the recommended one.