Sitecore publish:end and publish:end:remote

In Sitecore, you have several ways of executing code after a publish. The publish:end and publish:end:remote events are the 2 most obvious choices.

There is a little confusion as to when in the publish pipeline these events are fired. In previous versions (prior to Sitecore 7.2), publish:end and publish:end:remote was fired for each language and each target, and publish:complete and publish:complete:remote was fired when the publish job was done. But in later versions, publish:end and publish:end:remote is also only fired once, when the current publish operation is completed.

The :remote events (publish:end:remote and publish:complete:remote) is fired on your remote (content delivery, or CD) servers.

Please note that the EventArgs are not the same for publish:end and publish:end:remote. So to properly handle publish events you need 2 different functions in your code.

To handle the publish:end event, you will need the following function:

public void PublishEnd(object sender, EventArgs args)
{
  var sitecoreArgs = args as Sitecore.Events.SitecoreEventArgs;
  if (sitecoreArgs == null)
    return;

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

  var rootItem = publisher.Options.RootItem;
  
  // Do code
}

To handle the publish:end:remote event, you need the following function:

public void PublishEndRemote(object sender, EventArgs args)
{
  var sitecoreArgs = args as Sitecore.Data.Events.PublishEndRemoteEventArgs;
  if (sitecoreArgs == null)
    return;

  Item rootItem = Factory.GetDatabase("web").GetItem(new ID(sitecoreArgs.RootItemId));
  
  // Do code
}

And in the configuration you need to point to the proper method:

<sitecore>
  <events>
    <event name="publish:end">
      <handler type="MyNamespace, MyDll" method="PublishEnd"/>
    </event>
    <event name="publish:end:remote">
      <handler type="MyNamespace, MyDll" method="PublishEndRemote"/>
    </event>
  </events>
</sitecore>

MORE TO READ:

Advertisements

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in c#, General .NET, Sitecore 7, Sitecore 8, Sitecore 9 and tagged , , . Bookmark the permalink.

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