Benjamin Tinker Team : Web Development

How to Intercept the Umbraco Publishing Events

Benjamin Tinker Team : Web Development

At times you may need to perform your own checks of content in Umbraco prior to it being commited to the database. This comes in handy when you want to perform some extra validation or implment a workflow process. There are three main events you can use for intercepting the save request from Umbraco:

  • ContentService.Saving
  • ContentService.SentToPublish
  • ContentService.Publishing

First you will need to set up a class that inherits from the ApplicationEventHandler and attach your events to it.

public class WorkflowEventHandler : ApplicationEventHandler
{
     public WorkflowEventHandler()
     {
         ContentService.Saving += ContentServiceOnSaving;
         ContentService.SentToPublish += ContentServiceOnSentToPublish;
         ContentService.Publishing += ContentServiceOnPublishing;
     }
...   

Now whenever content in Umbraco is either saved and/or published the relative event will be called.

private void ContentServiceOnSaving(IContentService sender, SaveEventArgs<IContent> saveEventArgs)
{
  ...
}

The ContentServiceOnSaving event is used prior to content being saved and published to the live site. No changes have been made to the node while executing this method. If you choose to cancel out of the method by using the cancelFunc delegate Umbraco will not save any changes made to the Node. This is very useful for blocking content that may fail your internal checks that do not come out of the box.

private void ContentServiceOnSentToPublish(IContentService sender, SendToPublishEventArgs<IContent> saveEventArgs)
{
  ...
}

The ContentSeviceOnSentToPublish event is called when a Editor who does not have publish rights saves content to be published. The ContentServiceOnSaving is called prior to this method so all checks in that method must be passed before this event can be accessed.

private void ContentServiceOnPublishing(IPublishingStrategy sender, PublishEventArgs<IContent> saveEventArgs)
{
  ...
}

Finally the ContentServiceOnPublishing event is called when a Node is set to be published. This is the final check before a Node is pushed to the live site. At this point you assume the content has been validated in ContentServiceOnSaving. Upon exiting this method the Node will be pushed to the live site.

By intercepting the Save and Publish methods in Umbraco you can lock down content and provide a more robust validation process for your Nodes. This can be expanded on to cater for client requirements of workflow notifications, word replacement filtering and post editing formatting. More power to developers.