In this tutorial, you will learn about Episerver events, what they are and how you can get Episerver to trigger some code you create when the event is fired. In this tutorial, this custom code will prevent certain Episerver CMS pages from being deleted. When content editors use the CMS, a number of events are constantly being fired in the background. Common events include on-page save, on-page publish, and on-page delete. As developers, we can hook into these events in order to run our own business logic. If you want to learn how to add this capability into your own project, read on 🔥🔥🔥
Today we will focus on the
DeletingContent event. In order to hook into an event, you need to register an event handler with the CMS when it boots up. To do that you will need to create a custom
InitializationModule that is defined in your project will get called on application start. In the module, we will register an event handler that will get triggered whenever a content editor attempts to delete a page within the CMS. Within that method, we will then need to check for the page type being deleted and prevent it from deleting if its of a certain type.
Preventing A Hard Delete
In Episerver, we have two ways of restricting what content can be deleted. First, I will cover how to stop items from being permanently deleted. This type of deletion is known as a hard delete. This method will allow editors to move pages to the trash, however, it will stop the item from being removed from the trash completely. The code to do this is shown below:
Initialize method, the
IContentEvents API is used to register an event handler with Episerver. The
IContentEvents API will give you access to register any event within the Episerver pipeline. On Line 5, an event is registered for `DeletingContent. It is within this method that we will implement the business logic to restrict what can be deleted.
DeletingContent you can use the
ContentEventArgs.Content property to get access to the current Episerver item that has been requested to be deleted. You can use the .NET
is operator to check the item type. You want to ensure the page type does not match the custom type we are trying to prevent from being deleted. If it does you can cancel the deletion request by enabling the
In this example, I'm preventing every page from being deleted. In a real scenario, I would assume you would only want to restrict certain page types, like a settings page from being deleted.
On Line 24, a display message is set. This message will be displayed to the content editor within the Episerver editor after the event completes. The purpose of this message is to make it clear to the editor why the page can not be deleted.
Preventing A Soft Delete / Moving An Item To The Trash Completely
It is also possible to prevent a content editor from moving an item into the trash in the first place. The code to do accomplish this is shown below:
This code is similar to the previous example. The big difference is that instead of hooking into the
DeletingContent event, you hook into the
MovingContent will be called whenever any page is moved inside of the CMS. In this second example, you need to check that the destination is not the trash can. Within Episerver, the trashcan will have a content reference Id of
2. To prevent content from being moved to the trash, we simply need to check the requested destination does not equal
2. You can see this check on Line 21,
e.TargetLink.ID == 2 😊
You now know two approaches that will prevent Episerver pages from being deleted within the CMS. If you want to prevent pages from being deleted, you will need to hook into the Episerver's event pipeline by using the
IContentEvents API and register an event method. The event method will be triggered when an associated event in Episerver is performed. It is as simple as that. Happy Coding 🤘