我在Sitecore项目中遇到问题。当我们在团队中工作时,我们无法跟踪所有已创建的项目以及他们给出的名称。问题是,人们正在创建具有相同名称的项目。这会在将项目移动到不同环境时造成一些严重问题。
我想要的是,在创建Sitecore项目时,管道方法应该执行并验证其直接父项是否已具有相同的项目名称。
例如:Parent A
有3个子项称为Child1, Child2, Child3
,当开发人员尝试创建名称为Child2
的项目时,弹出/警报应显示,并且不允许他创建项目
请帮助我。
答案 0 :(得分:12)
您可以将自己的处理程序添加到item:creating
事件,并检查父级是否已包含具有建议名称的子级。
这是描述how to prevent duplicates items in Sitecore的好文章。我从那里复制了以下代码:
<event name="item:creating">
<handler type="YourNameSpace.PreventDuplicates, YourAssembly" method="OnItemCreating" />
</event>
namespace YourNamespace
{
public class PreventDuplicates
{
public void OnItemCreating(object sender, EventArgs args)
{
using (new SecurityDisabler())
{
ItemCreatingEventArgs arg = Event.ExtractParameter(args, 0) as ItemCreatingEventArgs;
if ((arg != null) && (Sitecore.Context.Site.Name == "shell"))
{
foreach (Item currentItem in arg.Parent.GetChildren())
{
if ((arg.ItemName.Replace(' ', '-').ToLower() == currentItem.Name.ToLower())
&& (arg.ItemId != currentItem.ID))
{
((SitecoreEventArgs)args).Result.Cancel = true;
Sitecore.Context.ClientPage.ClientResponse.Alert
("Name " + currentItem.Name + " is already in use.Please use another name for the page.");
return;
}
}
}
}
}
}
}
答案 1 :(得分:0)
我有一篇博文,其中使用了项目创建/保存事件,并使用索引搜索来识别重复项。这是使用Sitecore 7.2实现和测试的。这是使用的配置:
<sitecore>
<events>
<event name="item:creating">
<handler type="MySite.Customizations.Customized_Sitecore.UniqueItemNameValidator, MySite" method="OnItemCreating" />
</event>
<event name="item:saving">
<handler type="MySite.Customizations.Customized_Sitecore.UniqueItemNameValidator, MySite" method="OnItemSaving" />
</event>
</events>
</sitecore>