Today's post shows how to add Default Layout to your Sitecore devices. This can be handy if you don't want to tediously update multiple templates / items just to configure the same layout for the new device over and over.
A good example of that is enabling RSS output for every item: it's natural to use a separate device/layout for that, but the layout is the same for all items in the solution. So instead of modifying templates and items, httpRequestBegin pipeline processor is used:
public class DefaultLayoutResolver
{
private static readonly ID xmlLayoutID = ID.Parse("{8CF08867-5789-40CA-A0A4-01D059E50E11}");
public void Process(HttpRequestArgs args)
{
if ((Context.Device != null) && (Context.Device.Name.ToLowerInvariant() == "xml") &&
(Context.Item != null) && (Context.Item.Visualization.Layout == null))
{
LayoutItem layout = Context.Database.Items[xmlLayoutID];
if (layout != null)
{
Context.Page.SetLayout(layout);
}
}
}
}
Add the processor right after the standard Sitecore LayoutResolver processor, so that Sitecore configuration takes precedence.
The same code is reused in Printers Inc to output raw item XML. For more flexibility, you can add the "Default Layout" lookup field to a device template and modify the DefaultLayoutResolver to read that value.