How to Hook Into The Episerver CMS Events Pipeline

A common requirement when working with Episerver, is to add custom functionality into the pages 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 content for your web sites search.

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 lists of all the events you can hook into here

So lets 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)
{
}
private void PublishedContent(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 PublishedContent(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

More Posts

5 replies
  1. James Wilkinson
    James Wilkinson says:

    Thanks Jon, useful article.
    Do you know of any ways of hooking into the CMS login event? I.e. so when a user logs in to the CMS I want to execute some code (e.g. fire an email – “user x just logged into the CMS”?

    Cheers 🙂

    Reply
    • Jon D Jones
      Jon D Jones says:

      It all depends on how you implement your membership and architect your registration/log-in. If you’re using the out-of-the-box one, you could extend the membership provider and hook into the on successful log-in method depending on your requirements The other way is to create your own log-in block and put all the logic in your code after checking for success/failure. My advice with this is to follow the standard .NET advice, as EPi doesn’t change that much

      Reply

Trackbacks & Pingbacks

  1. […] have talked previously in, How to Hook Into The EpiServer CMS Events Pipeline about the EpiServer event pipeline. When we work with EpiServer content, a number of events are […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *