Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically removing sublayouts in Sitecore

Does anyone know how to remove the renderings from a Sitecore item?

I want to remove all of the sublayouts so I can replace them with a new set. I have tried this but it does not seem to work. Nothing changes on the item.

I appear to be able to get the renderings like this:

RenderingReference[] renderings = item.Visualization.GetRenderings(Sitecore.Context.Device, true);

But there appears to be no way to then set them.

I can also get the renderings like this (from the link above):

LayoutDefinition layoutDefinition = LayoutDefinition.Parse(LayoutField.GetFieldValue(item.Fields[Sitecore.FieldIDs.LayoutField]));
DeviceDefinition device = layoutDefinition.GetDevice(Sitecore.Context.Device.ID.ToString());

if (device.Layout != null) device.Layout = null;
if (device.Renderings != null) device.Renderings = new ArrayList();

But again this does not work. Clearing the device from the layoutDefinition and setting the modified one has led to this exception: No connection could be made because the target machine actively refused it. And I now cannot view the item at all!

I feel like I'm barking up the wrong tree, any ideas?

Using Sitecore 6.4

UPDATE Re: techphoria414

Code I tried:

layoutDefinition.Devices.Clear();
layoutDefinition.Devices.Add(device);
like image 602
Jon Avatar asked Feb 21 '23 05:02

Jon


1 Answers

I think your exception is unrelated. To actually save your changes, you need to edit the item. Be sure you always access and update the value throgh LayoutField.Value.

LayoutField layoutField = new LayoutField(item.Fields[Sitecore.FieldIDs.LayoutField]);
LayoutDefinition layout = LayoutDefinition.Parse(layoutField.Value);
//make your changes to the LayoutDefinition here
item.Editing.BeginEdit();
layoutField.Value = layout.ToXml();
item.Editing.EndEdit();
like image 106
nickwesselman Avatar answered Mar 06 '23 10:03

nickwesselman