How to Hook Into The Episerver CMS Events Pipeline

A common requirement when working with Episerver is to add custom functionality into the page's events, for example, you may need to run custom code when you save, delete or move content. One example that springs to mind, is re-indexing your website's search index. Luckily for us, Episerver provides a very easy provider called the Content Event provider to allow us to perform these sorts of tasks.

IContentEvents

In order to hook into Episerver CMS events, you will need to create an initialization module. If you have never come across one of these before, then I would recommend that you read this article first. We access the Content Events Provider the same way we do with any other Episerver API, via the ServiceLocator, like so:
   var events = ServiceLocator.Current.GetInstance<IContentEvents>();
After you have access to the content events provider, you will have access to override a number of very useful events that get triggered within the CMS, like the PublishedContent, DeletingContent, MovedContent and SavingContent events to name a few. You can see a full list of all the events you can hook into here So let's take a look at the code we need to do this:
    [InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule),
                      typeof(EPiServer.Web.InitializationModule))]
    public class EventsInitialization : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            var events = ServiceLocator.Current.GetInstance<IContentEvents>();
            events.PublishedContent += EventsPublishedContent;
        }
 
        public void Preload(string[] parameters)
        {
        }
 
        public void Uninitialize(InitializationEngine context)
        {
        }
 
        EventsPublishedContent(object sender, ContentEventArgs e)
        {
            /// Do stuff here
        }
    }
NOTE You can also hook into Episerver commerce events the same way, as long as you add the:
typeof(EPiServer.Commerce.Initialization.InitializationModule)
Within the ModuleDependency attribute. The code above is pretty simple, we create an initialization module, call ServiceLocator with IContentEvents and then register the events you are interested in. Once you have your events registered the methods will be hit when a content editor tries to perform one of the matching options. One standard thing you will very likely need to do is get access to the Episerver page or block that is attempting to be modified. There are several ways of doing this:

private void EventsPublishedContent(object sender, ContentEventArgs e) 
{ 
     var repo = ServiceLocator.Current.GetInstance<IContentRepository>(); 
     var content = repo.Get<IContent>(e.ContentLink); 
} 
This snippet will give you access to the objects IContent properties. If you want the whole object you can simply type it yourself like this:
   var pageType = eventArgs.Content as EpiPageType;

Conclusion

In today's guide, we've talked about how you can hook into the Episerver CMS event pipeline. If you create an InitializationModule and use IContentEvents you can get access to a number of very useful events, like hooking into the Saving, Updating and Deleting event handlers. When you get into your code you will very likely want to get access to the object being manipulated. You can use the ContentEventArgs to get access to it.


Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge


Back to top
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-35662136-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();