Skip to content

Instantly share code, notes, and snippets.

@asmagin
Created May 15, 2014 06:08
Show Gist options
  • Select an option

  • Save asmagin/70336fb5982fdf139579 to your computer and use it in GitHub Desktop.

Select an option

Save asmagin/70336fb5982fdf139579 to your computer and use it in GitHub Desktop.
Sitecore MVC: Layout with Device Fall Back
namespace Example
{
using System.Collections.Generic;
using System.Linq;
using Sitecore;
using Sitecore.Data.Items;
using Sitecore.Mvc.Extensions;
using Sitecore.Mvc.Pipelines.Response.GetPageRendering;
using Sitecore.Mvc.Presentation;
public class GetLayoutRenderingWithFallback : GetLayoutRendering
{
protected override Rendering SelectLayoutRendering(List<Rendering> renderings, GetPageRenderingArgs args)
{
var result = base.SelectLayoutRendering(renderings, args);
if (result != null)
{
return result;
}
var device = Context.Device;
while (device != null)
{
var b = device.ValueOrDefault((DeviceItem d) => d.ID.ToGuid());
foreach (var current in renderings.Where(current => current.DeviceId == b && current.RenderingType == "Layout"))
{
// set fallback device as current.
Context.Device = device;
return current;
}
device = Context.Device.FallbackDevice;
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment