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:
- Sitecore custom cache that is cleared on publish by briancaos
- Sitecore Events from Sitecore Community Docs
- Generic way to get Item and Database on publish:end and publish:end:remote by Sitecore Basics
