【问题标题】:How to avoid duplicate name while creating Sitecore item创建 Sitecore 项目时如何避免重复名称
【发布时间】:2015-01-26 11:27:01
【问题描述】:

我的 Sitecore 项目遇到问题。由于我们在一个团队中工作,我们无法跟踪创建的所有项目以及他们给出的名称。问题是,人们正在创建具有相同名称的项目。在将物品移动到不同的环境时,这会导致一些严重的问题。

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

例如:Parent A 有 3 个名为 Child1, Child2, Child3 的子项,当开发人员尝试创建名称为 Child2 的项时,应显示弹出窗口/警报,并且不允许他创建该项。

请帮帮我。

【问题讨论】:

    标签: sitecore sitecore6 sitecore7 sitecore-mvc


    【解决方案1】:

    您可以将自己的处理程序添加到 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;
                }
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 该解决方案不会发现重命名项目的问题,它只检查项目何时创建。
    【解决方案2】:

    我为此发布了一篇博客文章,它使用项目创建/保存事件并使用索引搜索来识别重复项。这是使用 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>
    

    【讨论】:

      猜你喜欢
      • 2017-09-27
      • 1970-01-01
      • 1970-01-01
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 2012-04-14
      • 1970-01-01
      相关资源
      最近更新 更多