问题
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);
回答1:
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();
来源:https://stackoverflow.com/questions/9604819/programmatically-removing-sublayouts-in-sitecore