Ash8087
Ash8087

Reputation: 701

Get IDs of Items to be Published from Sitecore Publishing Processor

I am trying write a custom publishing processor in Sitecore so that I can log the exact date and time that any item is published. The code I am using (which I adapted from Uli Welterbach's code here) does not throw an error but it does not seem to give me the IDs of the items that are actually getting published. What am I doing wrong?

 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();
        }

Upvotes: 1

Views: 926

Answers (2)

Debajit Mukhopadhyay
Debajit Mukhopadhyay

Reputation: 4182

You can try context.ProcessedItemIds

Upvotes: 0

Hishaam Namooya
Hishaam Namooya

Reputation: 1081

You should use the publish:itemProcessing pipeline. This is triggered when an item is being published. You will obtain the item id. I have a blog post where by I have make use of the publish:itemProcessed, which is triggered after an item has been published. The link is https://hishaamn.wordpress.com/2016/04/07/sitecore-incremental-publish-summary/

The implementation will be similar

Code Sample

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;
   }
}

Config

For Sitecore 7.x, the publish:itemProcessing is found in the Web.config and for Sitecore 8.x, the publish:itemProcessing is found in the Sitecore.config from the App_Config folder Thanks

Upvotes: 1

Related Questions