Sitecore Rendering Template Custom Datasource

Using the feature of a rendering template enables to allow user to select values when configuring a sublayout on an item. For example, you are displaying content in form of boxes and you want that each boxes to have a specific size.

Hard-coding the query in the Source field of the rendering template is useful only if you have a single site. But for multi-sites, the source value of the Rendering Template should be dynamic.

When to Hard-Code

If you have different sites and they all require the same value from the Rendering Template, then you can hard code the query in the Source.

Making the Source of Rendering Template Dynamic

In order to make the source dynamic, you should make use of a custom token that will fetch the items and populates your Custom Field Type. So, in brief, the following are required:

  1. Custom Field Type.
  2. Custom Token.

Creating The Dynamic Rendering Template

Scenario

In this post, I will take the example of inserting box sizes based on site. Programme A will have Box Size 1 and Box Size 2 for its Sites, example Site A while programme B will have Box Size 3 and Box Size 4 for its Sites, example Site B. I will be using a custom droplink and the custom token $boxsize.

Design

The Content Tree will look as shown in the snapshot below:

Content Tree.PNG

By making use of the Insert Option, the Box Size Container will only allow creation of Box Size item when clicking on the Insert Button.

Implementation

In order for the custom token to work, you need to first create a custom field type. Below is the steps on how to create the droplink

  • Create a new class called CustomDropLink which inherits Sitecore LookupEx.
  • Override the DoRender Method.
  • Change the Source of the base method.

There are many articles on the web explaining on how to create a custom droplink. If you have any issues, please leave a comment so that I may help you.

The implementation of the custom token is much easier. You will just need to change the Programme based on the item path. Example:

If you select Item 1, its path will be /sitecore/content/Programme A/Site A/Item 1 and if Item 4 is selected, its path will be /sitecore/content/Programme B/Site B/Item 4. Only the Programme and Site are changing. So you may have something as below:

/sitecore/content/[programmeName]/[siteName]/Box Size Container

Then you can do a simple replace as follows:

var path = "/sitecore/content/[programmeName]/[siteName]/Box Size Container";

var finalPath = path.Replace("[programmeName]", "yourItemProgramme")
                    .Replace("[siteName]", "yourItemSiteName");

This will act as the Source of your custom droplink. Now that the custom token and custom field type has been implemented, you need to override the Sitecore Layout Details to set a cookie to store the Item ID since in the Properties Window, it does not know the item context. So you will need to pass the ID to the Properties window. Just before the custom field type is rendered, on its OnLoad method, you will need to fetch the item, calculates the path and finally, assign it to the Source of the Droplink.

For more information on how to override the Sitecore Layout Details, please read my post Override Sitecore Layout Details

Advertisements

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 )

Google+ photo

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

Connecting to %s