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:

[sourcecode language=”csharp”]
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);

}

[/sourcecode]

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:

[sourcecode language=”csharp”]

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);

}

[/sourcecode]

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:

[sourcecode language=”xml”]

<?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>
[/sourcecode]

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