从Sitecore项目中删除渲染

时间:2016-10-13 12:10:17

标签: sitecore rendering removeall

我必须通过渲染id删除所有渲染实例。我试过下面的代码,但它没有用。

LayoutField layoutField = new LayoutField(item.Fields[Sitecore.FieldIDs.LayoutField]);
LayoutDefinition layoutDefinition = LayoutDefinition.Parse(layoutField.Value);
DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(Sitecore.Context.Device.ID.ToString());

//Create a RenderingDefinition and add the reference of sublayout or rendering
RenderingDefinition renderingDefinition = new RenderingDefinition();
renderingDefinition.ItemID = sublayoutId;

var d = layoutDefinition.GetDevice(deviceDefinition.ID.ToString());
d.Renderings.Remove(renderingDefinition);

item.Editing.BeginEdit();
layoutField.Value = layoutDefinition.ToXml();
item.Editing.EndEdit();

我提到了:

1 个答案:

答案 0 :(得分:5)

请尝试以下代码。我没有测试它,但是没有创建一个RenderingDefinition对象并将其从列表中删除(这将无法工作,因为它不是对象的相同实例),它会过滤现有的渲染数组,比较{{ 1}} property:

ItemId