如何以编程方式向 Sitecore 8 中的渲染添加条件

How to programmatically add conditions to a rendering in Sitecore 8

我正在以编程方式将渲染添加到项目占位符。 代码如下所示:

var newRendering = new Sitecore.Layouts.RenderingDefinition();
newRendering.ItemID = "{SUBLAYOUTID}";
newRendering.Placeholder = "PLACEHOLDER";
newRendering.Datasource = "{DATASOURCEID}";

//Adds the new RenderingDefinition to the device renderings.
device.AddRendering(newRendering);

这行得通。我也在尝试添加条件,但不知道如何添加。 有人有想法吗?

你应该使用 newRendering.Rules 属性 但不幸的是它是 XmlElement 类型所以生成正确的值不会那么容易,除非你想从另一个渲染复制条件.

Rules 属性 的示例值为:

<ruleset>
  <rule uid="{40E52E8C-39EB-40A2-BBE4-985F7421EDEC}" p2:name="Condition Name" xmlns:p2="s">
    <conditions>
      <condition uid="0BE3D7A7B0C64002A02AC1205567C43E" p2:id="{8A9B001F-FB59-4F0F-B3F3-C6C5360ED451}" p2:Now="20150128T143000" />
    </conditions>
    <actions>
      <action uid="295430EFD38B40A7AE307A5C7751F0C1" p2:id="{0F3C6BEC-E56B-4875-93D7-2846A75881D2}" p2:DataSource="{86902BC8-BFE6-4376-87B0-81EFE87BDE34}" />
    </actions>
  </rule>
  <rule uid="{00000000-0000-0000-0000-000000000000}" p2:name="Default" xmlns:p2="s">
    <conditions>
      <condition uid="7EF464FFDF5E45FCA84732B375F2FA3A" p2:id="{4888ABBB-F17D-4485-B14B-842413F88732}" />
    </conditions>
    <actions />
  </rule>
</ruleset>