We've talked about setting the device programmatically and when it can be handy. Another trick that goes in the same category is layout substitution. Say we're building a mobile version of the web site and we want exactly the same set of sublayouts and renderings as in the desktop version, but placed on a different layout so that renderings can be rearranged freely using the placeholders.
Similar to the device resolver, we build a custom layout resolver processor to customize Sitecore's httpRequestBegin pipeline with out logic. The flow here is simple: if visitor uses handheld device, switch to a predefined layout.
public class LayoutResolver
{
if (IsMobileRequest())
{
using (new SecurityDisabler())
{
LayoutItem mobileLayout = Context.Database.GetItem(ID.Parse("{C10DBF03-F810-40A9-8ACB-5354428CC6BF}"));
if (mobileLayout != null)
{
Tracer.Info("Overriding layout to mobile layout");
// Change the layout to mobile layout
Context.Page.FilePath = mobileLayout.FilePath;
}
else
{
// writes error to Sitecore trace, visible in debug mode
Tracer.Error("Mobile layout not found");
}
}
}
}
Make sure to place your layout resolver after the standard one, so that you override Sitecore's choice of layout and not vice versa:
<processor type="Sitecore.Pipelines.HttpRequest.LayoutResolver, Sitecore.Kernel" />
<!-- Sets mobile layout for requests from mobile devices -->
<processor type="Sitecore.Website.Pipelines.LayoutResolver, Sitecore.Website" />