Navigation and service panel


Content

How to collect publishing data

By Zahid Nawaz on 11. June 2015, No comments

In this post I will explain, how you can collect the publishing data by writing custom event handlers in Sitecore

In one of my customer projects I have a use case to collect the publishing data (like what has been published and who was the publisher) for an external data archiving service. The archive service keeps track of the provided information and starts crawling the public website to archive the affected pages. This must all happen after the data has been successfully published and effected the HTML caches has been cleared.

The required data about what has been published or who was the publisher is completely available on the Content Management Server (CM). On a Content Delivery Server (CD) you have a smart Publisher object which does not contain all the mentioned information. There are few more limitation on CD Server like you can not find the list of all published items, which you can easily maintain at CM Server. Therefore I need to use both CM and CD to collect all required data and send a notification to the external service.

For this purpose I have subscribed my own custom event handler in both the publish:end and publish:end:remote events after the HTML Cache clear handler. On the CM Server publish:end handler, i collect all needed data and save it into a Sitecore Item in a UGC (user generated content) database. At the CD publish:end:remote event handler I read the data from the commonly used Sitecore Item and send a notification to the external service.

Sitecore internally publishes each item of a bundle individually. The publish:end event will be triggered once at the end when all items have been published and therefore does not contain the list of all the items, published languages or versions. If you are interested in collectint this information you need to implement a handler in the publish:itemProcessed event where you can maintain a list of languages and versions of each published item.

Here is the code for my custom event handler which is collecting the required publishing data.

public void GetPublishingInfo(object sender, EventArgs args)
{
    Publisher publisher = Event.ExtractParameter(args, 0) as Publisher;
    if (publisher == null)
    {
        Log.Info("Cancel Process: Publisher Data is Null.", this);
        return null;
    }

    // if publishing is not on Web Target than do nothing
    if (publisher.Options == null ||
        publisher.Options.TargetDatabase == null ||
        string.IsNullOrEmpty(publisher.Options.TargetDatabase.Name))
    {
        Log.Info("Cancel Process: Publisher.Options Data is Null.", this);
        return null;
    }

    if (publisher.Options.TargetDatabase.Name != "web")
    {
        Log.Info("Cancel Process: Publishing TargetDatabase :" + publisher.Options.TargetDatabase.Name, this);
        return null;
    }

    Job job = JobManager.GetJob(publisher.GetJobName());

    PublishingInfo pInfo = new PublishingInfo()
    {
        PublishBatchId = publisher.Options.RecoveryId,
        RootItem = this.GetCustomPublishItem(publisher.Options.RootItem),
        Deep = publisher.Options.Deep,
        PublishMode = publisher.Options.Mode,
        RepublishAll = publisher.Options.RepublishAll,
        PublistDateTime = publisher.Options.PublishDate,
        PublishUser = = job.Options.ContextUser.LocalName.ToLower()           
    };            
    // Store PublishingInfo into Sitecore item in UGC DB        
}

To register this handler just add it to the publish:end event in web.config or your custom configuration files under App_Config/include/

<event name="publish:end">
    <handler type="Unic.CustomCode.Events.AfterCacheClearedHandler, Unic.CustomCode" method="GetPublishingInfo"/>
</event>

A similar kind of Custom Event Handler should be registered for the publish:end:remote Event, which reads the Publishing Information from the commonly used Item, processes the data and sends a notification to the external service.

No comments

Add your comment

Your email address will not be published. Required fields are marked *

*