Sitecore 8:更改子布局的数据源模板

时间:2015-12-02 06:47:51

标签: templates sitecore sitecore8 sublayout

我对子布局的数据源模板的更改有疑问。现在,有两个子布局: Sub1 Sub2 ,它们具有 Template1 作为其数据源模板。直到我发现我需要 Sub1 的不同数据源模板时,我已经创建了许多子布局Sub1和Sub2。

Template2 现在将替换Template1 作为子布局Sub1的数据源模板。 现在,我必须将使用sublayout创建的所有项目的模板更改为Sub1。

问题是我必须通过内容编辑器手动更改每个项目的模板 - >配置 - >更改模板技术,这非常麻烦。有没有其他方法可以同时更改所有这些项目的模板?

3 个答案:

答案 0 :(得分:5)

我建议您安装Sitecore PowerShell Extensions并使用Sitecore PowerShell控制台更改模板。

binutils-2_25_1

答案 1 :(得分:3)

正如@SitecoreClimber建议的那样,执行此操作的最佳方法是安装Sitecore PowerShell Extensions并使用PowerShell进行修复。如果您没有管理员权限,或者您不允许在计算机上安装PowerShell扩展,则可以使用以下.NET代码来实现您的目标。只需将ID变量的值替换为模板和子布局的ID:

// replace with the first template's ID
ID template1ID = new ID("{A0F73C76-DD4D-4037-90D4-48B616397F5D}");

// replace with the second template's ID
ID template2ID = new ID("{43A1EBB0-CABB-4682-9F5B-7765D7FB0E29}");

// replace with your sublayout's ID
ID sublayout2ID = new ID("{1C6094FA-4539-48E4-A24A-104787641A88}");
Database masterDatabase = Factory.GetDatabase("master");

TemplateItem template2Item = masterDatabase.GetTemplate(template2ID);

// Set to your RootItem
Item rootItem = masterDatabase.GetItem("{756B23C8-1C0F-41AC-9273-B18FDA047925}");

using (new SecurityDisabler())
{
    foreach (Item child in rootItem.Axes.GetDescendants())
    {
        RenderingReference[] renderings = child.Visualization.GetRenderings(Sitecore.Context.Device, true);

        IEnumerable<RenderingReference> sublayout2Renderings =
            renderings.Where(x => x.RenderingID == sublayout2ID);

        foreach (RenderingReference rendering in sublayout2Renderings)
        {
            if (!string.IsNullOrEmpty(rendering.Settings.DataSource))
            {
                Item datasourceItem = masterDatabase.GetItem(rendering.Settings.DataSource);

                if (datasourceItem != null)
                {
                    if (datasourceItem.TemplateID == template1ID)
                    {
                        datasourceItem.ChangeTemplate(template2Item);
                    }
                }
            }
        }
    }
}

答案 2 :(得分:3)

还有另外一种方法 - 如果您安装了Sitecore Rocks,您可以多选所有项目,右键单击并选择更改模板 - 无代码,并且非常快,除非您的内容项目在许多不同的地方。