Skip to content

Instantly share code, notes, and snippets.

@PetersonDave
Last active December 22, 2015 07:59
Show Gist options
  • Save PetersonDave/6441921 to your computer and use it in GitHub Desktop.
Save PetersonDave/6441921 to your computer and use it in GitHub Desktop.
Move a rendering
// Move renderings example usage
var rendering = device.Renderings.Cast<RenderingDefinition>().SingleOrDefault(r => r.ItemID == MyRenderingId);
device.MoveRenderingUp(rendering);
item[Sitecore.FieldIDs.LayoutField] = layoutDefinition.ToXml();
// DeviceDefinition Extensions
public static class DeviceDefinitionExtensions
{
public static void MoveRenderingDown(this DeviceDefinition deviceDefinition, RenderingDefinition renderingDefinition)
{
Sitecore.Diagnostics.Assert.IsNotNull(renderingDefinition, "renderingDefinition cannot be null");
Sitecore.Diagnostics.Assert.IsNotNull(deviceDefinition.Renderings, "device renderings cannot be null");
var index = deviceDefinition.Renderings.IndexOf(renderingDefinition);
bool isIndexValid = index == deviceDefinition.Renderings.Count - 1;
Sitecore.Diagnostics.Assert.IsTrue(isIndexValid, "renderingDefinition is at the last position. Cannot be moved.");
deviceDefinition.MoveRendering(renderingDefinition, index + 1);
}
public static void MoveRenderingUp(this DeviceDefinition deviceDefinition, RenderingDefinition renderingDefinition)
{
Sitecore.Diagnostics.Assert.IsNotNull(renderingDefinition, "renderingDefinition cannot be null");
Sitecore.Diagnostics.Assert.IsNotNull(deviceDefinition.Renderings, "device renderings cannot be null");
var index = deviceDefinition.Renderings.IndexOf(renderingDefinition);
Sitecore.Diagnostics.Assert.IsTrue(index != 0, "renderingDefinition is at the first position. Cannot be moved.");
deviceDefinition.MoveRendering(renderingDefinition, index - 1);
}
public static void MoveRendering(this DeviceDefinition deviceDefinition, RenderingDefinition renderingDefinition, int position)
{
Sitecore.Diagnostics.Assert.IsNotNull(renderingDefinition, "renderingDefinition cannot be null");
Sitecore.Diagnostics.Assert.IsNotNull(deviceDefinition.Renderings, "device renderings cannot be null");
bool isPositionValid = position >= 0 && position < deviceDefinition.Renderings.Count;
Sitecore.Diagnostics.Assert.IsTrue(isPositionValid, "position is outside the bounds of the rendering collection");
var index = deviceDefinition.Renderings.IndexOf(renderingDefinition);
Sitecore.Diagnostics.Assert.IsTrue(index >= 0, "renderingDefinition does not exist in the redering collection of the specified device");
deviceDefinition.Renderings.Remove(renderingDefinition);
deviceDefinition.Renderings.Insert(position, renderingDefinition);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment