The following code will remove rendering from items. It works fine but the changes happen only in the Shared Layout
.
Is there a way to read/update renderings in only a certain layout type - Shared
or Final
.
LayoutField layoutField = new LayoutField(targetItem.Fields[FieldIDs.LayoutField]);
LayoutDefinition layoutDefinition = LayoutDefinition.Parse(layoutField.Value);
DeviceDefinition deviceDefinition = layoutDefinition.GetDevice("{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}");
DeviceDefinition ddef = layoutDefinition.GetDevice(deviceDefinition.ID.ToString());
/// Get the array of all renderings for the target page item
IEnumerable<RenderingDefinition> renderingsArray = ddef.Renderings.ToArray().Cast<RenderingDefinition>();
ddef.Renderings = new ArrayList(renderingsArray.Where(r => r.ItemID != someRenderingId).ToList());
/// Save the layout changes
using (new SecurityDisabler())
{
targetItem.Editing.BeginEdit();
layoutField.Value = layoutDefinition.ToXml();
targetItem.Editing.EndEdit();
}
FieldIDs.FinalLayoutField
. LayoutField layoutField = new LayoutField(targetItem.Fields[FieldIDs.FinalLayoutField]);