Programmatically removing sublayouts in Sitecore

心已入冬 提交于 2019-12-31 02:03:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!