gpt4 book ai didi

c# - Sitecore 插入规则以确保最多 (1) 个特定类型的 child

转载 作者:太空狗 更新时间:2023-10-29 23:09:24 25 4
gpt4 key购买 nike

sitecore 中有没有办法确保某种类型的项目只能有 1 个子项目?我正在阅读规则引擎说明书,但没有获得太多详细信息。

最佳答案

我工作过的一个网站要求某个项目类型下不能存在超过 6 个子项目。我们考虑过使用插入选项规则,但决定放弃这个想法,因为它不能阻止复制、移动或复制项目。

相反,我们决定使用专门用于此任务的处理程序来扩展 item:created 事件。下面是它如何工作的精简示例。一个明显的改进是从父项的字段中获取最大子项限制(当然,仅对管理员可见)。您甚至可以在这里利用规则引擎...

public void OnItemCreated(object sender, EventArgs args)
{
var createdArgs = Event.ExtractParameter(args, 0) as ItemCreatedEventArgs;

Sitecore.Diagnostics.Assert.IsNotNull(createdArgs, "args");
if (createdArgs != null)
{
Sitecore.Diagnostics.Assert.IsNotNull(createdArgs.Item, "item");
if (createdArgs.Item != null)
{
var item = createdArgs.Item;

// NOTE: you may want to do additional tests here to ensure that the item
// descends from /sitecore/content/home
if (item.Parent != null &&
item.Parent.TemplateName == "Your Template" &&
item.Parent.Children.Count() > 6)
{
// Delete the item, warn user
SheerResponse.Alert(
String.Format("Sorry, you cannot add more than 6 items to {0}.",
item.Parent.Name), new string[0]);
item.Delete();
}
}
}
}

关于c# - Sitecore 插入规则以确保最多 (1) 个特定类型的 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12185870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com