如何更新特定 Sitecore 模板的多个已存在项目的字段值?

How to update field value for multiple already existing items of a specific Sitecore template?

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

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

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

1.Doing 从 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 字段。用您需要的任何值替换这些值。

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

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

就批量编辑项目而言,@martin-miles 的任一解决方案都可以。我可能会选择 PowerShell 方法,但请记住,它是一种锋利的工具,因此值得在执行完整 运行 之前在较小的数据集上测试您的命令或脚本。另一个要考虑的选项是 Revolver 工具。这是更改当前目录下所有后代的示例:

find -r (sf bucketable 1)