Sitecore Sunday Pt 1: Programmatic Layout Assignment

It has been some time since I last updated this blog and it has been quite a year and a half! It all started when my family and I decided that after 5 years of living in Toronto and working for nonlinear creations it was time to move back to the UK. I was also in the middle of delivering a large project during the move and it wasn’t long after I got back to the UK that I began helping nonlinear start the nonlinear London office! For those of you who are familiar with nonlinear creations you may also be aware that nonlinear was acquired by Valtech in June.

So this is all basically a reason (read excuse) for my dip in contributions to the Sitecore community with the exception of presenting Sitecore XP at Scale at the London user group in May. It is also the motivation to get involved again and it starts with this blog series entitled Sitecore Sundays. The idea is simple, write a Sitecore blog post every Sunday for a 100 Sundays (100 always seems like a nice round number and a challenge).

So without further ado let’s get into the first post in the series, programmatic layout assignment.

The standard way that Sitecore “presents” a page is by resolving the context item (the page) and then inspecting the renderings (shared) and final renderings (not shared) field of the context item and its templates standard values. This is all known as resolving layout deltas and is a fairly complex subject that I won’t go into in detail now but follow this link for a flow chart of the process.

But what if we want to resolve the presentation that should be used for the context item programmatically based on some business logic? What if the context item has no presentation details associated with its templates standard values (think data repositories)? What if we want to expose a subset of products to a version of the presentation that contains a specific personalisation scenario?

Well, it turns out there’s a pipeline for that (disclaimer: this applies to MVC only, but we’re all using MVC now, right?). It’s in Sitecore.MVC.config and by default it looks like this:

<mvc.getXmlBasedLayoutDefinition>
  <processor type="Sitecore.Mvc.Pipelines.Response.GetXmlBasedLayoutDefinition.GetFromLayoutField, Sitecore.Mvc"/>
</mvc.getXmlBasedLayoutDefinition>

The GetFromLayoutField processor inherits from GetXmlBasedLayoutDefinitionProcessor which takes in a GetXmlBasedLayoutDefinitionArgs parameter in its constructor that has one really important property, ContextItem. Essentially if ContextItem is set on the args it will use that item to get the presentation details instead of the default, which is the standard context item:

protected virtual XElement GetFromField(GetXmlBasedLayoutDefinitionArgs args)
{
    Item contextItem = args.ContextItem ?? PageContext.Current.Item;
    
    if (contextItem == null)
    {
        return null;
    }

    return this.GetFromField(contextItem);
}

So this gives us an opportunity to place a processor in the pipeline that runs before the default processor and sets the ContextItem on the args based on some business logic. Et voila, dynamic presentation resolution that means the presentation item and the context item do not have to be the same thing. There are lots of possibilities with this.

Until next Sunday!