In this tutorial, you will learn how to hook into the Episerver page event pipeline. This can be handy if you want to run some custom code when a content editor saves, deletes or moves content within the CMS. One common example is re-indexing your website's search index on a page published. Luckily for us, Episerver provides a very easy way to hook into these requests using the ContentEvent provider. If you want to learn how to master this API, read on 🔥🔥🔥

Content Events

In order to hook into Episerver CMS events, you will need to create an Initialization Module. Within the class we can add some code to hook into the request pipeline. There are two approaches to hook into the pipeline. The first is on a per content basis, using Locate.ContentEvents():

The second and probably more recommend approach is globally, using the IContentEvents API and accessing it via dependency injection:

After you have access to the content events provider, you will have access to hook into a number of very useful CMS events, like PublishedContent, DeletingContent, MovedContent and SavingContent You can see a full list of all the events you can hook into here. The code required to hook into an event is shown below:

You can also hook into Episerver Commerce events using the same technique:

To hok into an event, create an initialization module, call ServiceLocator with IContentEvents as T and register the event handlers that you are interested in. Once you have registered your methods with an event, the code will be called whenever a content editor tries to perform one of the associated actions within the CMS. One bit of data that you will very likely need within this code, is the Episerver page or block that triggered the call. You can access this data using ContentEventArgs:

This will give you access to the object's types as an IContent. If you want the whole object you can simply type it yourself:

You now know how to hook into the Episerver CMS event pipeline. Happy Coding 🤘