1

我正在编写一个管道处理器以在复制项目后将其两个字段置空以避免重复的自动生成字段:

  1. 项目a被复制到项目中b
  2. 项目b已删除一个或多个字段的值

我为此设置了一个类,但只能a通过ClientPipelineArgs参数访问原始项目id

有没有办法捕获ID新复制的项目b

下面的代码:

namespace XXXX.SC.Pipeline
{
    public class DuplicateItem
    {
        public void StripShortIDs(ClientPipelineArgs args)
        {
            Item item = Sitecore.Configuration.Factory.GetDatabase(args.Parameters["database"]).GetItem(args.Parameters["id"]);

            if (!String.IsNullOrWhiteSpace(item["Short ID"]))
                using (new EditContext(item))
                {
                    item["Short ID"] = String.Empty;
                }
        }

        public void StripStatus(ClientPipelineArgs args)
        {
            Item item = Sitecore.Configuration.Factory.GetDatabase(args.Parameters["database"]).GetItem(args.Parameters["id"]);

            if (item.TemplateID.ToString() == ConfigurationManager.AppSettings["config key redacted"])
                using (new EditContext(item))
                {
                    item["Status"] = "0";
                }
        }
    }
}
4

1 回答 1

4

假设您正在使用uiDuplicateItem管道,默认情况下定义为

<uiDuplicateItem>
  <processor mode="on" type="Sitecore.Shell.Framework.Pipelines.DuplicateItem,Sitecore.Kernel" method="CheckPermissions" />
  <processor mode="on" type="Sitecore.Shell.Framework.Pipelines.DuplicateItem,Sitecore.Kernel" method="GetName" />
  <processor mode="on" type="Sitecore.Shell.Framework.Pipelines.DuplicateItem,Sitecore.Kernel" method="Execute" />
</uiDuplicateItem>

您需要Execute先通过单行更改来覆盖处理器。下面是更新后的 Execute 方法的代码,更新的地方注释了:

public void Execute(ClientPipelineArgs args)
{
  Assert.ArgumentNotNull((object) args, "args");
  Database database = Factory.GetDatabase(args.Parameters["database"]);
  Assert.IsNotNull((object) database, args.Parameters["database"]);
  string str = args.Parameters["id"];
  Language result;
  if (!Language.TryParse(args.Parameters["language"], out result))
    result = Context.Language;
  Item obj = database.GetItem(ID.Parse(str), result);
  if (obj == null)
  {
    SheerResponse.Alert("Item not found.", new string[0]);
    args.AbortPipeline();
  }
  else
  {
    Item parent = obj.Parent;
    if (parent == null)
    {
      SheerResponse.Alert("Cannot duplicate the root item.", new string[0]);
      args.AbortPipeline();
    }
    else if (parent.Access.CanCreate())
    {
      Log.Audit((object) this, "Duplicate item: {0}", new string[1]
      {
        AuditFormatter.FormatItem(obj)
      });


      // this was the original code - duplicated item created but id not stored
      // Context.Workflow.DuplicateItem(obj, args.Parameters["name"]);

      // new code with the id stored in args.Parameters["duplicatedId"]
      Item duplicated = Context.Workflow.DuplicateItem(obj, args.Parameters["name"]);
      args.Parameters["duplicatedId"] = duplicated.ID.ToString();


    }
    else
    {
      SheerResponse.Alert(Translate.Text("You do not have permission to duplicate \"{0}\".", new object[1]
      {
        (object) obj.DisplayName
      }), new string[0]);
      args.AbortPipeline();
    }
  }
}

然后,您可以在新处理器之后添加您的处理器以清除字段,Execute并从args.Parameters["duplicatedId"].

于 2013-05-23T07:47:32.180 回答