6

我有一个自定义站点核心按钮,它可以更改当前项目的模板,非常简单。

但是,作为其中的一部分,我还尝试将旧布局的渲染迁移到新布局(如果它属于某个子布局类型)ItemId。但是ItemId返回的总是空的,我从返回的唯一值RenderingDefinitionUniqueId.

我究竟做错了什么?

我已将此博客文章用作指南。

编码

public class ConvertToNewTemplateCommand : Command
{
protected void Run(ClientPipelineArgs args)
{
    if (!SheerResponse.CheckModified())
        return;

    Item item = Context.ContentDatabase.Items[args.Parameters["id"]];
    if (args.IsPostBack)
    {
        if (args.Result == "yes")
        {
            //Get current layout details
            var originalLayoutXml = item[FieldIDs.LayoutField];

            //Get new template
            TemplateItem hubTemplate = Context.ContentDatabase.GetTemplate("some guid...");
            //Change template  
            item.ChangeTemplate(hubTemplate);
            //Reset laytout
            ResetLayout(item);
            //Get reset layout
            var newLayoutXml = item[FieldIDs.LayoutField];

            //Add all the module containers to the new layout in the central column
            MoveModuleContainers(item, originalLayoutXml, newLayoutXml);
        }
    }
}

private void MoveModuleContainers(Item item, string oldXml, string newXml)
{
    var oldLayout = LayoutDefinition.Parse(oldXml);
    var newLayout = LayoutDefinition.Parse(newXml);

    bool updated = false;

    var oldRenderings = (oldLayout.Devices[0] as DeviceDefinition).Renderings;
    var newRenderings = (newLayout.Devices[0] as DeviceDefinition).Renderings;

    foreach (RenderingDefinition rendering in oldRenderings)
    {
        // Here is where the rendering.ItemID is always null
        if (rendering != null && !String.IsNullOrEmpty(rendering.ItemID) && new Guid(rendering.ItemID) == new Guid("matching guid..."))
        {
            rendering.Placeholder = "middlecolumn";
            newRenderings.Add(rendering);
            updated = true;
        }
    }

    if (updated)
    {
                   // Save item...
            }
}
}
4

1 回答 1

7

最后我得到了Sitecore 支持,它告诉我应该使用:

Sitecore.Data.Fields.LayoutField.GetFieldValue(item.Fields[Sitecore.FieldIDs.LayoutField])

代替:

item[FieldIDs.LayoutField]

正确获取物品layoutField。这导致渲染值被正确解析,正如他们所说,其余的都是历史。

于 2012-12-06T15:30:39.950 回答