Publishing Service: BulkPublishingEnd pipeline

Sitecore Publishing Service replaces the existing Sitecore publishing methods with a faster, reliable and user-friendly solution. It reduces the amount of time spent publishing large volumes of items and provides better visual feedback to the user. It was released for Sitecore 8.2 and is also available for Sitecore 9.

In this post, I am going to write about the publishEndResultBatch pipeline. An event pipeline that Sitecore starts after Sitecore Publishing Service finishes publishing each batch of items.

You can find more information about installing and configuring Sitecore Publishing Service on Sitecore documentation. Also, I recommend you reading Jonathan Robbins blog post Sitecore Publishing Service – Setting up 8.2s new Publishing option.

The publishEndResultBatch pipeline

At the end of a publishing, Sitecore Publishing Service passes the results to the publishEndResultBatch pipeline. Developers can hook into this pipeline to work with these results. You can update site maps with the publishing results, for example, or automatically send an email whenever something goes wrong.

In order to hook into the publishEndResultBatch pipeline, firstly you have to create a class with a method named Process taking PublishEndResultBatchArgs as parameter.

The PublishEndResultBatchArgs is available at the Sitecore.Publishing.Service.Pipelines.BulkPublishingEnd namespace from Sitecore.Publishing.Service.dll.

Here is a sample code.

You can find the Sitecore.Publishing.Service.dll in the Sitecore Publishing Module 3.1.0 rev. 171220.zip installation package. There is no Nuget package available for this assembly by the time I wrote this post.

Next, you have add the following patch to a configuration file in your project.

Sending email after a publishing job is finished

Here is a sample code for sending email. Read the next section for more details about the PublishEndResultBatchArgs.

using Sitecore;
using Sitecore.Publishing.Service.Pipelines.BulkPublishingEnd;
using System.Net.Mail;
using System.Linq;
using System;

namespace Sandbox.Foundation.PublishingService.Pipelines.PublishEndResultBatch
{
    public class PersistEndResultBatch
    {

        public string MailFrom { get; set; }
        public string MailTo { get; set; }

        public void Process(PublishEndResultBatchArgs args)
        {


            if (args.Aborted)
            {
                SendAbortMessage(args);
            }
            else
            {
                SendDetailsMessage(args);
            }

        }

        private void SendDetailsMessage(PublishEndResultBatchArgs args)
        {
            MailMessage message = new MailMessage(this.MailFrom, this.MailTo);
            message.Subject = $"Publishing job {args.JobData.JobId} finished at {DateTime.Now}";
            var itemsAffected = args.Batch.Select(b => b.EntityId).Distinct().Count();
            message.Body = "Finished publishing job!\n";
            message.Body += $"({itemsAffected}) items were published\n";
            message.Body += $"Date: {args.JobData.PublishDate}\n";
            message.Body += $"Job Id: {args.JobData.JobId}\n";
            message.Body += $"Username: {args.JobData.Username}\n";
            MainUtil.SendMail(message);
        }

        private void SendAbortMessage(PublishEndResultBatchArgs args)
        {
            MailMessage message = new MailMessage(this.MailFrom, this.MailTo);
            message.Subject = $"Publishing job {args.JobData.JobId} aborted at {DateTime.Now}";
            message.Body = $"A publishing job was just aborted. Job Id: {args.JobData.JobId} \n Message: {args.Message}";
            MainUtil.SendMail(message);
        }

    }
}

What should you know about PublishEndResultBatchArgs?

The args.JobData member provides you with an overview of the publishing job. Here is some of the information you can get out of JobData property.

  • JobId is a unique identifier for the job. A job can contain one or more batches.
  • LanguageNames shows one or more target publishing languages. Before starting publish job, you select the target languages you want to publish.
  • Metadata contains more options related to the publishing job such as username, publishing targets, clear caches (true/false), the type of publishing (full publish, publish item), include descendants (true/false), etc.
  • Other useful members are PublishDate, SourceDatabaseName (usually “master”), status (Complete, Failed, Cancelled, etc).

The args.Batch members gives you information about the published items. From that you can get the item’s ID through the EntityId property, for example. Also you can identify whether that item was added, changed or deleted.

Important! When you add a new version to an item, it will show twice in the args.Batch. One as a deleted item, another as a created item.

That is why I use the code snippet below to select unique items and count them.

var itemsAffected =
args.Batch.Select(b => b.EntityId).Distinct().Count();

In this post, I briefly described what you can do with the BulkPublishingEnd event pipeline and some information and heads up on the PublishEndResultBatchArgs.

You can find the sample code for sending email on my GitHub repository.

comments powered by Disqus