Inheriting renderings in Sitecore

A typical page layout contains an aside column beside the main content.

These aside columns typically contain spots, boxes or whatever you will call them.

asidespot

We call them spots at Pentia.

What is the problem then? When using the page editor for inserting and personalizing renderings then there is a 1 – 1 relation with the rendering reference that is inserted and the item.

Inserting aside spots on each and every page is extremely time consuming for editors and might also exceed their Sitecore knowledge if they’re not trained.

What if the spot renderings simply could be inherited from the parent item? So it is not necessary to put a spot on a sub-sub-sub-sub page but still keep the aside column filled with dynamic content? This was easy before the page editor and personalization became the Sitecore way, back then it was almost just inheriting a field value but today it is a bit more complicated.

The aside column is just an example. The same inheritance mechanism shown below can also be applicable on other placeholders in which you would like renderings to be inherited from the parent or another ancestor of the current item.

Inheriting renderings from ancestors

First we create a new user control (or rendering when using MVC) containing a placeholder for our aside spots.

Then we register the sublayout/rendering in Sitecore and save the ID in an Constants file.

In this example we just use a “hardcoded” placeholder key in the Constants file called “Constants.Keys.AsideSpotsKey” for the placeholder key in which we want to inherit renderings from the parent.

Next step is to create a processor for the insertRenderings pipeline.

A bit of explanation. First we check if the current item has renderings at all (see this post), then we check if we are in Edit mode (the page editor) and then we check if inheritance is skipped on the item, more on this later in the post.

If the checks does not exit the processor then a check is made if the item already has renderings in a placeholder with that key. 

If the item does not have any renderings in the placeholder already then we call a service class with the item parent as argument along with the rendering references already found in the insertRenderings pipeline, the sublayout ID and the placeholder key.

The Inherit method is called recursively until a condition fails or until some renderings to inherit is found. These conditions are if the item to inherit from is null or if it is the start item of the site. It is also checked if the inheritance should be skipped on that item as also mentioned above.

If renderings are found on the item to inherit from then they  are inserted into the current items rendering collection which was passed as a parameter to the Inherit method.

Now to making the SkipInherit part, it is only a checkbox so should be fast.

Make a base class in Sitecore and let all pages where rendering inheritance should be enabled inherit from that.

spotinheritance

Last but not least we need to place the AddFallbackRenderings processor in the insertRenderings pipeline. To make it work we need to patch the processor just before  Sitecore.Pipelines.InsertRenderings.Processors.EvaluateConditions

That is it, renderings are inherited.

This code could easily be generalized and made into a module which read the placeholder keys and rendering ID’s from a settings item. The use of static methods and internals should be revised then to make it flexible and extendible.

Another approach could be to make a set of actions and conditions for the rules engine. I am working a bit on this approach so a blog post will follow on this some time soon.

Let me know if you like the rules idea and want to contribute or if you like me to share the source code from this post, just write a comment.

Anders Laub

Anders Laub Christoffersen

Anders has been working with Sitecore for over a decade and has in this time been the lead developer and architect on several large scale enterprise solutions all around the world. Anders has been nominated a Sitecore Technical MVP three years in a row for 2014, 2015 and 2016. Anders is now working as a Sr. Solutions Architect at Sitecore in Copenhagen.

One thought on “Inheriting renderings in Sitecore

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*
Website