如何更新特定Sitecore模板的多个现有项目的字段值?

时间:2015-08-12 00:13:23

标签: templates field sitecore

在Sitecore 7.2中,我有一个包含许多项目的文件夹,我需要将其转换为Buckets。我检查了该模板的标准值中的bucketable(根据官方文档),因此所有新项目都进入了存储桶,但我还需要在几百个现有项目中更新该字段。这样做的聪明方法是什么?

此外,之前我遇到类似的情况,当我需要为某些模板的所有现有项目更新禁用字段时,可能有一些通用的解决方案吗?

2 个答案:

答案 0 :(得分:5)

据我所知,有两种方法可以做到这一点 - 直接使用代码,使用PowerShell模块智能(如果安装了一个)。不幸的是,我不认为Sitecore Desktop中有特定的界面可以做到这一点。

1.从C#代码进行批量更新:

private void UpdateAllFieldsRecursively(Item parentItem, string templateName, string fieldName, string newValue)
{
    if (parentItem != null)
    {
        using (new SecurityDisabler())
        {
            foreach (Item childItem in parentItem.Children)
            {
                if (childItem.Fields[fieldName] != null && childItem.TemplateName == templateName)
                {
                    using (new EditContext(childItem))
                    {
                        childItem[fieldName] = newValue;
                    }
                }

                if (childItem.HasChildren)
                {
                    UpdateAllFieldsRecursively(childItem, templateName, fieldName, newValue);
                }
            }
        }
    }
}

// and below there is the call to recursive mehod
const string parentNode = "/sitecore/content";
var database = Sitecore.Context.Database;
var parentItem = database.GetItem(parentNode);

UpdateAllFieldsRecursively(parentItem, "Article", "Bucketable", "1");

2.PowerShell ISE 。执行以下代码(您可能需要根据您的方案稍微修改一下):

cd 'master:/sitecore/content'
Get-ChildItem -Recurse . | Where-Object { $_.TemplateName -match "Article" -and $_.Fields["Bucketable"] -ne $null } | ForEach-Object {

    $_.Editing.BeginEdit()
    $_.Fields["Bucketable"].Value = "1";
    $_.Editing.EndEdit()
    ""
}

此脚本从/ sitecore / content递归递送,并更新所有文章模板的Bucketable字段。用你需要的任何东西替换这些值。

这是一篇博客文章,描述了:http://blog.martinmiles.net/post/mass-update-of-field-value-for-all-existing-items-of-certain-template-two-ways-of-achieving-result

答案 1 :(得分:2)

对于您的存储桶方案,如果我理解正确,您只需要将包含现有项目的文件夹更新为存储桶。听起来你已经正确配置了它。您无需转到每个现有项目并更新Bucketable设置。如果您已将其设置为标准值,则只需要同步存储桶,Sitecore将负责其余部分。

enter image description here

在批量编辑项目方面,@ martin-miles'解决方案将有效。我可能会选择PowerShell方法,但要记住它是一个很好的工具,因此在进行完整运行之前,值得在较小的数据集上测试命令或脚本。另一个需要考虑的选择是Revolver工具。这是更改当前目录下所有后代的示例:

find -r (sf bucketable 1)