如何在创建Sitecore项目时避免重复名称

PaR*_*RsH 4 sitecore sitecore6 sitecore7 sitecore-mvc

我在Sitecore项目中遇到问题.当我们在团队中工作时,我们无法跟踪所有已创建的项目以及他们给出的名称.问题是,人们正在创建具有相同名称的项目.这会在将项目移动到不同环境时造成一些严重问题.

我想要的是,在创建Sitecore项时,管道方法应该执行并验证其直接父项是否已具有相同的项名称.

例如:Parent A有3个子项被调用Child1, Child2, Child3,当开发人员尝试创建一个名称Child2为弹出/警报应显示的项目时,不允许他创建项目.

请帮我解决一下这个.

Mar*_*lak 12

您可以将自己的处理程序添加到item:creating事件中,并检查父项是否已包含具有建议名称的子项.

这篇文章描述了如何防止Sitecore中的重复项目.我从那里复制了以下代码:

<event name="item:creating">
  <handler type="YourNameSpace.PreventDuplicates, YourAssembly" method="OnItemCreating" />
</event>
Run Code Online (Sandbox Code Playgroud)
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;
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)