Keystone for Sitecore: Extending template entities

keystone-logoThe Keystone for Sitecore business layer provides generated template entities for each page and component template. These entities provide properties to access each field with a strongly-typed value, as well as properties for template identifiers, field identifiers, and field names. As you extend the Keystone templates and add new fields, you will want to extend these classes for use in your components.

As an example, the AccordionContainer entity class is used for the Accordion component template. It contains strongly-typed properties for accessing the child accordion panes and a boolean property for determining if multiple panels are allowed to be open at the same time. This class has been implemented as a partial class to allow for you to extend it for your own needs. For example, if you wanted to add a new checkbox field named “Enable Halloween”, you would follow these steps:

  1. Add field to template
    Use the Template Manager to add the checkbox field to the Accordion Container template, as you would with any template.
  2. Create a partial class
    In your business layer, create a new partial class with the same name as the AccordionContainer class.

    namespace Keystone.SBL.Templates.Components{
       public partial class AccordionContainer {
       }
    }
  3. Add a property for the field
    In the new AccordionContainer partial class, define a new property for your field. Place business logic to retrieve your field value and return it.

    public bool EnableHalloween {
       get { return InnerItem.Fields["Enable Halloween"] == "1";}
    }
  4. Use in your sublayout/view
    The template entities take a Sitecore item in their constructor, so you can instantiate the object with your datasource item to begin accessing the property.

    var accordion = new AccordionContainer(DataSourceItem);
    var halloweenEnabled = accordion.EnableHalloween;
    

You can also extend the entities to add other things such as helper methods.  These can then be used for business logic that manipulates field data and related item information.

Does this work for MVC or only for WebForms?

Both the MVC and WebForms versions of the Keystone components make use of the same template entities. For WebForms, these objects are instantiated in the ASCX code-behind. For MVC, the components initialize the entity via the Controller and View Model classes.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s