Sitecore Publishing Events publish:complete and publish:end

As you’ve probably heard by now Sitecore 7.2 will introduce a number of changes to how the publishing process works.  While there’s been a fair amount of deserved fanfare around this there have been other features previously introduced that have gone virtually unnoticed.  Today I’ll be covering a useful little event called publish:complete.

Before we get into the details of publish:complete I’ll do a quick recap of the publish:end event.  When a content author publishes an item, the publish:end event will be called each time Sitecore publishes a language to each publishing target*.  This is an incredibly handy tool to have in your tool kit.

EDIT:  Older versions of Sitecore will raise a publish:end event for each language published.  Sitecore 7.2 has changed this and will raise this event only when each publishing target when it completes.  Thanks Erwin Raets and Sitecore support.

In our example we’ll be publishing an item to 3 different languages.

Using this code:

public void OnPublishEnd(object sender, EventArgs args)

{

var sitecoreArgs = args as Sitecore.Events.SitecoreEventArgs;

if (sitecoreArgs == null) return;

var publisher = sitecoreArgs.Parameters[0] as Publisher;

if (publisher == null) return;

var rootItem = publisher.Options.RootItem;

Log.Debug(string.Format("OnPublishEnd: Item {0}, version: {1}, language, {2} just got published to {3}! Subitems: {4}", rootItem.Name, rootItem.Version, rootItem.Language, publisher.Options.TargetDatabase, publisher.Options.Deep), this);

}

Will produce the following:ManagedPoolThread #2 09:53:05 DEBUG OnPublishEnd: Item Home, version: 1, language, en just got published to web! Subitems: TrueManagedPoolThread #17 09:53:05 DEBUG OnPublishEnd: Item Home, version: 1, language, es-ES just got published to web! Subitems: TrueManagedPoolThread #3 09:53:05 DEBUG OnPublishEnd: Item Home, version: 1, language, fr-CA just got published to web! Subitems: TrueAs you can see it our “OnPublishEnd” event was invoked 3 times, once for each language.While publish:end gives you a chance to take an action each time a different language is published, there may be times when it’s not preferable to have this run multiple times.  This is where publish:complete comes into play.  The publish:complete event is only invoked once, when all of the languages and publishing targets have been published to.You’re still able to access a lot of the same information that was provided to the publish:end event.  This is passed as an enumeration of “Sitecore.Publishing.DistributedPublishOptions.”Using this code:


public void OnPublishComplete(object sender, EventArgs args)

{

var sitecoreArgs = args as Sitecore.Events.SitecoreEventArgs;

if (sitecoreArgs == null) return;

var publishingOptions = sitecoreArgs.Parameters[0] as IEnumerable<DistributedPublishOptions>;

if (publishingOptions == null) return;

Log.Debug(string.Format("OnPublishComplete: languages: {0}", string.Join(", ", publishingOptions.Select(x => x.LanguageName))), this);

}

Will produce:ManagedPoolThread #0 09:53:05 DEBUG OnPublishComplete: languages: en, es-ES, fr-CAOur method “OnPublishComplete” is only called once and we’re still able to determine all of the languages we’re publishing to.  There’s other information provided as well, it was just getting a little long winded for a demo; I encourage you to explore this yourself.Going forward, using publish:end will still have its place, it’s just nice to know there is another option if need be.If you’re playing along at home you might want this config as well:


<?xml version="1.0"?>

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">

<sitecore>

<events>

<event name="publish:complete">

<handler type="Foo.Bar, Test" method="OnPublishComplete" />

</event>

<event name="publish:end">

<handler type="Foo.Bar, Test" method="OnPublishEnd" />

</event>

</events>

</sitecore>

</configuration>

More reading:

http://sdn.sitecore.net/articles/api/using%20events.aspx

http://sdn.sitecore.net/Products/Sitecore%20V5/Sitecore%20CMS%207/ReleaseNotes/ChangeLog/Release%20History%20SC72.aspx

4 thoughts on “Sitecore Publishing Events publish:complete and publish:end

Add yours

  1. The described behavior is correct till Sitecore 7.1. In Sitecore 7.2, the publishing process has been changed. The publish:end event is raised not for each language is published, but for each publishing target is finished.

  2. Hi there,

    The information is not quite correct, as of Sitecore 7.2, the publisher publishes all languages in 1 go, which means the publish:end event in NOT called for every language anymore (it was indeed so for all prior versions of Sitecore).
    The publish:end event is now fired after every publishing target, but since the publisher only has a private field containing all the languages it published to, it is not easy to get a hold of them…
    I used the publish:complete event since I only have 1 target anyway, and then you can easily loop over the languages and targets…

    Cheers!
    Erwin

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

Blog at WordPress.com.

Up ↑

%d bloggers like this: