API + Sitecore Graphic

Customization using Sitecore Publishing Pipeline

Posted 07/12/2016 by Cynthiya Rani

I recently used the Sitecore Publish Pipeline to do a data integration with sitecore content and custom database. This “how-to” might be helpful to others trying to do the same.

As per the requirement, the published new products to be synced and notified to the Product Owner. The best way to sync the data is to plug into the process that Sitecore used to populate the web database. Sitecore uses the publish pipeline to migrate published items from the master to the web database.

Pipelines define a sequence of processors that implement a function, such as defining the Sitecore context for an HTTP request or generating a list of messages in the Content Editor.
Each processor in a pipeline contains a method named Process() that accepts a single argument and returns void. The type of the argument to the Process() method may be of a type specific to the pipeline or the default Sitecore.Pipelines.PipelineArgs. To create a pipeline processor, create a class that implements a method named Process() with the same signature as the other processors in the pipeline. Your processor can inherit from an existing processor, and you can add, remove, replace and rearrange processors in the pipelines to suit your requirements.

The PublishProcessor object is located in the Sitecore.Publishing.Pipelines.Publish namespace. We need to inherit from this object to your own class and override the Process(PublishContext) method with our own custom functionality.

namespaceSampleSitecore.Custom.Products
{
      public class PopulateProducts: PublishProcessor {
            public override void Process(PublishContext context) {
                   foreach (var itemId in GetPublishQueueItems(context.PublishOptions))
                   {
                             //do custom functionality 
                    }
             }
      }
     private IEnumerable<ID> GetPublishQueueItems(PublishOptions options)
     {
           if (options.Mode == PublishMode.Incremental)
                 return PublishQueue.GetPublishQueue(options).Select(product => product.ItemId).ToArray();
           else
                 return PublishQueue.GetContentBranch(options).Select(product => product.ItemId).ToArray();
       }
}

The PublishContext that’s passed by Sitecore’s publishing processor has a list of items being published. All we have to do is iterate through the list and use the Sitecore Item & its property to manipulate as per our requirement.

Lastly, we need to attach this custom processor to the publish processor via configuration. Patching the publishing pipeline 
changes are as follows:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <pipelines>
            <publish help="Processors should derive from Sitecore.Publishing.Pipelines.Publish.PublishProcessor">
                <processor patch:after="processor[@type='Sitecore.Publishing.Pipelines.Publish, Sitecore.Kernel']"   type="SampleSitecore.SCExtensions.Products.PopulateProducts, SampleSitecore.SCExtensions"  />
            </publish>
        </pipelines>
    </sitecore>
</configuration>

I hope someone finds this to be helpful when designing a near real-time integration.

Share:

Archive

Syndication