Customizing system objects with custom data or objects |
|||||
Customizing system objects with custom data or objects |
|
||
You can extend system objects in Kentico CMS by adding your own fields (i.e. properties).
To do this, create a custom data class containing the required fields and connect it to the appropriate system object. All standard system objects implement the IRelatedData interface, which allows you to connect any type of object:
•Through the RelatedData property
•Dynamically by handling the OnLoadRelatedData event
If the connected object implements the IDataContainer interface, the data stored in the custom fields can then be accessed as part of the system object via the API (GetValue and SetValue methods) or macro expressions.
The following sections demonstrate how to extend the SiteInfo object, which represents sites in Kentico CMS. The example adds two custom properties for sites:
•SiteOwner (string)
•SiteValidUntil (DateTime)
1. Open your web project in Visual Studio.
2. Create a new class named SiteRegistrationData.cs.
oPlace the class file into the App_Code folder if your Kentico CMS installation is a Web site project.
3. Write the code of the class.
oThe class must implement the IDataContainer interface.
oDefine your custom properties and all required IDataContainer members (as shown in the code below).
[C#]
using System; |
You need to bind the custom SiteRegistrationData class to the SiteInfo object. The example uses the OnLoadRelatedData event, which you can handle for specific object types, including SiteInfo objects. This type of binding is dynamic, which means that the system loads the data only when it is requested.
1. Create a new class file in the App_Code folder (or Old_App_Code if your project was installed as a web application).
2. Delete the default class declaration and instead extend the CMSModuleLoader partial class.
3. Create a new class inside the CMSModuleLoader that inherits from CMSLoaderAttribute.
4. Add the attribute defined by the internal class before the declaration of the CMSModuleLoader partial class.
5. Override the Init method inside the attribute class, and assign a handler to the SiteInfo.TYPEINFO.OnLoadRelatedData event.
6. Define the handler method for the OnLoadRelatedData event:
oThe handler must return an instance of your custom data class (with appropriate values assigned to the class's fields).
[C#]
using System; |
You can now work with the custom fields of site objects using the API.
For example, open your website in CMS Desk and add the following code into the ASCX layout of one of your website's pages:
<asp:Label runat="server" id="lblSiteInfo" /> <script runat="server"> protected void Page_PreRender(object sender, EventArgs e) { CMS.SiteProvider.SiteInfo currentSite = CMSContext.CurrentSite;
this.lblSiteInfo.Text = String.Format("Site '{0}' is valid until {1} and owned by {2}.", currentSite.DisplayName, currentSite.GetValue("SiteValidUntil"), currentSite.GetValue("SiteOwner")); } </script> |
The GetValue method allows you to retrieve the data stored in the custom properties, just like with native fields of the SiteInfo object.
The label control displays information on the page in the following format:
Site 'Corporate Site' is valid until 1/15/2013 1:00:00 PM and owned by John Smith.
Accessing properties through macros:
You can also load the values of custom properties using macro expressions. For example:
1.Add the Static text web part to the page (via the Design tab).
2.Copy the following text into the web part's Text property.
<ul> <li>Site: {% CurrentSite.SiteDisplayName %}</li> <li>Valid until: {% CurrentSite.SiteValidUntil %}</li> <li>Owned by: {% CurrentSite.SiteOwner %}</li> </ul> |
The web part displays a list of information about the current website, including the values of the custom fields.