Sitecore Custom Rendering Datasource

Sitecore Datasource is widely used across all projects. It helps in specifying what is the value a Sitecore Field can take or list of values from which the user will select from. For example, a Sitecore MultiList should have a datasource specified in the template so that the list of items can be available upon creation of the item. You can also specify a datasource in the rendering parameters window when adding a sublayout.

But what about custom rendering datasource? Let us take the following example:

You have 3 programmes of the same template. The sublayout to be added on the programme items has a datasource field in the rendering parameter which needs to allow the user to select only banners item. Below is a diagram which briefly describe how the content tree looks like


So, in order to be able to use a single sublayout which allows the user to select the Banner for each site respectively, you will require to implement a Custom Rendering Datasource.

The pipeline that you need to patch is the getRenderingDatasource. Below is the different steps that you will require to implement a custom rendering datasouce:

1. Sublayout

Click on your sublayout itema and scroll to the Editor Options Section, there is a field called Datasource Location. In this, you can specify the query you want, below is an example of the query:

query:ancestor-or-self::*[@@templatename='Website']//Banner Container

Note that the query above needs to have a template called Website then get the Banner Container.

2. Code

Here, it is where you will insert the logic that you will require to have a custom datasource. As example, the below code will execute and will set the window from where content authors select the item to a specific container. The below code will render the datasource and content authors will never be able to select banner from Programme 1 in Programme 2 or Programme 3

public void Process(GetRenderingDatasourceArgs args)
    Assert.IsNotNull(args, "args");

    string text = args.RenderingItem["Datasource Location"]; //Get the value from the field  
    if (!string.IsNullOrEmpty(text))
        if (text.StartsWith("query:") && !string.IsNullOrEmpty(args.ContextItemPath))
            var contextItem = args.ContentDatabase.GetItem(args.ContextItemPath);

            if (contextItem != null)
                text = text.Remove(0, 6); //remove the query:  
                var item = contextItem.Axes.SelectSingleItem(text); //Execute the query  

                if (item != null)

To restrict the content authors from selecting other templates, you can set the Datasource Template. This allows content authors to select items which are of the same template as specified in the Datasource Template

3. Config

Now you need to patch the getRenderingDatasource pipeline. Below is how you will need to patch it

    <processor type="YourNamespaceHere, YourAssemblyName"
               patch:before="processor[@type='Sitecore.Pipelines.GetRenderingDatasource.GetDatasourceLocation, Sitecore.Kernel']"/>



One thought on “Sitecore Custom Rendering Datasource

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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