获取要从Sitecore Publishing Processor发布的项目的ID

时间:2016-06-02 17:49:00

标签: sitecore sitecore8

我正在尝试在Sitecore中编写自定义发布处理器,以便我可以记录任何项目发布的确切日期和时间。我正在使用的代码(我改编自Uli Welterbach的代码here)不会抛出错误,但它似乎没有给我实际发布的项目的ID。我做错了什么?

 public class PublicationProcessor : PublishProcessor
    {        

        public override void Process(PublishContext context)
        {
            Assert.ArgumentNotNull(context, "context");                        
            var cqs = context.Queue.ToList();            

            string connectionString = "Data Source=MyServer;initial catalog=AuditHistory; User ID=sa;Password=123456789;Integrated Security=false;";
            var query = @"INSERT INTO [dbo].[ItemHistory]
                               ([ItemID])
                         VALUES
                               (@ItemId)";

            using (SqlConnection cn = new SqlConnection(connectionString))
            {

                foreach (var itemId in GetPublishQueueItems(context.PublishOptions))
                {
                    using (SqlCommand cmd = new SqlCommand(query, cn))
                    {
                        // define parameters and their values
                        cmd.Parameters.Add("@ItemId", SqlDbType.UniqueIdentifier, 50).Value = itemId.ToGuid();

                        // open connection, execute INSERT, close connection
                        cn.Open();
                        cmd.ExecuteNonQuery();
                        cn.Close();
                    }
                }

            }

        }

        private IEnumerable<ID> GetPublishQueueItems(PublishOptions options)
        {
            if (options.Mode == PublishMode.Incremental)
                return PublishQueue.GetPublishQueue(options).Select(candidate => candidate.ItemId).ToArray();
            return PublishQueue.GetContentBranch(options).Select(candidate => candidate.ItemId).ToArray();
        }

2 个答案:

答案 0 :(得分:1)

您应该使用publish:itemProcessing管道。这是在发布项目时触发的。您将获得项目ID。我有一篇博文,其中我使用了publish:itemProcessed,这是在项目发布后触发的。链接为https://hishaamn.wordpress.com/2016/04/07/sitecore-incremental-publish-summary/

实施将类似

代码示例

public void OnItemProcessing(object sender, EventArgs args)
{
   var itemArgs = args as ItemProcessingEventArgs;

   if (itemArgs != null)
   {
       //Retrieve the current Item being published;

       var itemId = itemArgs.Context.ItemId;

       //Your code here;
   }
}

<强>配置

对于Sitecore 7.x,publish:itemProcessing位于 Web.config 中,而对于Sitecore 8.x,publish:itemProcessing位于 Sitecore中来自App_Config文件夹的.config 感谢

答案 1 :(得分:0)

您可以尝试context.ProcessedItemIds