Even as the live mode is used more often, publishing remains one of the most important parts of the Sitecore world. If you look to the right, you’ll see my ugly drawing designed to contrast with the beauty of the new publishing architecture Ole implemented for Sitecore 6.
The entire publishing process is managed by the two pipelines: publish and publishItem. The publish pipeline runs once whenever a publish process is started from the user interface or programmatically. The pipeline collects all the items that need to be published, and runs the publishItem pipeline for each item separately.
PublishItem pipeline decides how to perform the actual publishing. It also raises two very useful events: publish:itemProcessing and publish:itemProcessed. Each event receives the full set of current publishing settings and the item being published.
When deciding between extending the publishItem pipeline and using the publishing events, I’d say use the events whenever reasonably possible. Publishing is still a low-level core process, so not having a chance to mess up the way the pipeline operates is good.
I’ll illustrate the new extensibility with two examples, one for each event.
As the name suggests, publish:itemProcessing event fires before the publishing is performed, and can be used to cancel the process. In this example, the publishing of protected items is forbidden.
public class EventHandler { public void ItemProcessing(object sender, EventArgs rawArgs) { // [1] – retrieving the args. var args = rawArgs as ItemProcessingEventArgs; Assert.IsNotNull(args, "args");
// [2] – action check if (args.Context.Action == PublishAction.DeleteTargetItem) { return; }
// [3] – retrieving the item being published var item = args.Context.PublishHelper.GetSourceItem(args.Context.ItemId); if (item == null) { return; }
// [4] – cancel publishing if the item is protected if (item.Appearance.ReadOnly) { args.Cancel = true; } } }
Things to note:
To activate the event:
<event name="publish:itemProcessing"> <handler method="ItemProcessing" type="Pipelines.Publishing.EventHandler, Pipelines" /> </event>
In the next post I’ll show how to publish related media items using the publish:itemProcessed event.
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.