Skip to content
August 29, 2014 / kiranpatils

Call External WebService If Item is REALLY Published

Challenge:

We were working on an interesting task. We’ve tree structure similar like this :

Scenario-v1Now, each product has retailers mapped to them, which gets stored in different system – To update that we’ve to call a Web Service and pass all the mapped retailers for particular product on PUBLISH – But we’ve to make sure that ONLY if the Item which is getting published should be of type PRODUCT. Sounds good?! Clear? Now, what you will use? Did you say publish:end? If yes, then we also thought it and gave it a try. It works fine when you publish an individual item. BUT what If when someone published root folder Products with Sub-items? If you said that If Current Publish Item is /Products/ then get all descendants and call web service for each one correct? Yes, it will work! But, let me add one more twist. What If, Someone did a Smart Publish? – Now, only products which has been updated and REALLY Got Published for them only Web Service should get called? I know you like the challenges! So, as we! We found a way [“A will will find a way!“]

Eager to know?! Here we go!

Solution :

Before we move ahead to the solution. I would recommend you to read following blog posts. They are Bible on Sitecore Publishing Pipelines:

  1. http://www.sitecore.net/Learn/Blogs/Technical-Blogs/John-West-Sitecore-Blog/Posts/2011/08/Intercept-Item-Publishing-with-the-Sitecore-ASPNET-CMS.aspx
  2. http://www.sitecore.net/Learn/Blogs/Technical-Blogs/John-West-Sitecore-Blog/Posts/2011/05/All-About-Pipelines-in-the-Sitecore-ASPNET-CMS.aspx

In the past when we were dealing with publishing challenges, We used Sitecore.Publishing.Pipelines.PublishItem.UpdateStatistics pipeline to understand whether current item got published or skipped by making traceToLog to true

<processor type="Sitecore.Publishing.Pipelines.PublishItem.UpdateStatistics, Sitecore.Kernel" runIfAborted="true">
     <traceToLog>true</traceToLog>
</processor>

And having this knowledge gave us a pointer to look in to. So, we just asked our common friend Reflector to show us what code lies within Sitecore.Publishing.Pipelines.PublishItem.UpdateStatistics.UpdateContextStatistics method, Which uses this code PublishItemResult result = context.Result;  PublishItemResult Class has property named as Operation which is of Type PublishOperation enum and it has following possible values:

public enum PublishOperation
{
    None, // Same as Skipped
    Skipped,// If Item has been skipped during publishing operation
    Created, // If Item has been created during publishing operation
    Updated, // If Item has been updated during publishing operation
    Deleted // If Item has been deleted during publishing operation

}

And here’s how the final code looks like:

   public class BasicUpdateRetailer
       : Sitecore.Publishing.Pipelines.PublishItem.PublishItemProcessor
{
    public override void Process(Sitecore.Publishing.Pipelines.PublishItem.PublishItemContext context)
    {
        PublishItemResult result = context.Result;
        Item itemToPublish = context.PublishHelper.GetItemToPublish(context.ItemId);
        if ((result != null) && (context != null))
        {
            switch (result.Operation)
            {
                case Sitecore.Publishing.PublishOperation.Created:
                case Sitecore.Publishing.PublishOperation.Updated:
                    if (itemToPublish.IsProduct())
                    {
                        Item[] targetItems = null;
                        MultilistField multilistField = itemToPublish.Fields["RETAILERSFIELD"];
                        if ((multilistField != null) && (multilistField.InnerField != null))
                            targetItems = multilistField.GetItems();

                        if (targetItems != null && targetItems.Any())
                        {
                            // Call WebService and do update
                        }
                    }
                    break;
                case Sitecore.Publishing.PublishOperation.Deleted:
                    break;
                case Sitecore.Publishing.PublishOperation.None:
                    break;
                case Sitecore.Publishing.PublishOperation.Skipped:
                    break;
                default:
                    break;
            }
        }
    }
}

Yes, As you rightly said we need to add our PublishItem Processor via configuration:

xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <publishItem>
        <processor patch:before="processor[@type='Sitecore.Publishing.Pipelines.PublishItem.UpdateStatistics, Sitecore.Kernel']"
                   type="Sitecore.Basics.Pipelines.PublishItem.BasicUpdateRetailer, Sitecore.Basics.Pipelines" />
      </publishItem>
    </pipelines>
  </sitecore>
</configuration>

You know other better way? Or you were looking for something and this helped? Feel free to share your learning(s) with us!

Good Reads:

  1. http://maxslabyak.com/sitecore/using-sitecore-publishing-pipeline-to-refresh-external-cdn-cache-2/

Happy Publishing! 🙂

Advertisements

2 Comments

Leave a Comment
  1. test / Sep 14 2014 4:25 pm

    test

    • kiranpatils / Sep 14 2014 4:31 pm

      Whomsoever you are — Your test is successful! 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: