-1

您好,我想为私人使用创建特殊页面,以便我可以更改每个渲染项的数据源值。我已经创建了下一个代码,但它不会保存对项目的任何更改。

  SC.Data.Database master = SC.Configuration.Factory.GetDatabase("master");
            SC.Data.Items.Item itm = master.GetItem(tbPath.Text);
            if (itm != null)
            {
                // Get the sublayout to update
                //string sublayout_name = txtSublayout.Text.Trim();
                //if (!string.IsNullOrEmpty(sublayout_name))
                {
                    // Get the renderings on the item
                    RenderingReference[] refs = itm.Visualization.GetRenderings(SC.Context.Device, true);
                    if (refs.Any())
                    {
                        //var data = refs.Select(d=>d);
                        //refs[0].Settings.DataSource
                        var sb = new StringBuilder();
                        using (new SC.SecurityModel.SecurityDisabler())
                        {
                            itm.Editing.BeginEdit();
                            foreach (var d in refs)
                            {
                                if (d.Settings.DataSource.Contains("/sitecore/content/Site Configuration/"))
                                {
                                    var newds = d.Settings.DataSource.Replace("/sitecore/content/Site Configuration/", "/sitecore/content/Site Configuration/" + tbLanguage.Text + "/");                                   
                                   // sb.AppendLine(string.Format("{0} old: {1} new: {2}<br/>", d.Placeholder, d.Settings.DataSource, newds));
                                    d.Settings.DataSource = newds;
                                }
                            }
                            itm.Editing.EndEdit();
                        }                        
                        //lblResult.Text = sb.ToString();

                }
            }
        }

我如何更改数据源?谢谢

4

3 回答 3

4

您在这里混合了 Sitecore 中的两种不同的东西。

  • 当 Sitecore 呈现页面时,在运行时分配给呈现的数据源
  • 分配给项目的展示详细信息的数据源

实现我认为您要实现的目标的最简单方法就是这样。

Item itm = database.GetItem("your item");
string presentationXml = itm["__renderings"];
itm.Editing.BeginEdit();
presentationXml.Replace("what you're looking for", "what you want to replace it with");
itm.Editing.EndEdit();

(我还没有编译和运行这段代码,但它应该可以正常工作)

于 2013-09-24T08:30:06.283 回答
1

您没有保存对该字段的更改。

使用 LayoutDefinitation 类解析布局字段,并为每个设备定义和渲染定义。

最后将 LayoutDifinition 提交到布局字段。

SC.Data.Items.Item itm = master.GetItem(tbPath.Text);
var layoutField = itm.Fields[Sitecore.FieldIDs.LayoutField];


LayoutDefinition layout = LayoutDefinition.Parse(layoutField.Value);
for (int i = 0; i < layout.Devices.Count; i++)
{
    DeviceDefinition device = layout.Devices[i] as DeviceDefinition;
    for (int j = 0; j < device.Renderings.Count; j++)
    {
        RenderingDefinition rendering = device.Renderings[j] as RenderingDefinition;

        rendering.Datasource = rendering.DataSource.Replace("/sitecore/content/Site Configuration/",
            "/sitecore/content/Site Configuration/" + tbLanguage.Text + "/");
    }
}

itm.Editing.BeginEdit();
var xml =layout.ToXml()

layoutField.Value = xml;
itm.Editing.EndEdit();

该代码不是 testet,而是从我在生产中的内容更改为在复制事件中替换数据源

于 2013-09-24T08:39:20.543 回答
0

如果有人在寻找单行 Linq :(已测试)

var layoutField = item.Fields[Sitecore.FieldIDs.LayoutField];
    if (layoutField != null)
    {
        var layout = LayoutDefinition.Parse(layoutField.Value);
        if (layout != null)
        {
            foreach (var rendering in layout.Devices.Cast<DeviceDefinition>()
                                     .SelectMany
                                     (device => device.Renderings.Cast<RenderingDefinition>()
                                     .Where
                                     (rendering => rendering.ItemID == "RenderingYoulooking")))
                                     {
                                         rendering.Datasource = "IDYouWantToInsert";
                                     }
            layoutField.Value = layout.ToXml();
        }
    }
于 2014-10-23T10:18:03.483 回答