I had a requirement on a recent project to store a reference to an Episerver page in a third-party database. This is fairly straightforward to achieve, serialize the content reference, then store it as a string within the database, however, as simple as it seems the process does throw an expected spanner in the works.

Before a page can be deleted within Episerver a check is made to ensure it is not referenced anywhere else within the CSM. When you store a reference externally to Episerver you lose this functionality and some custom code is required to prevent the deletion. In today's guide, we're going to cover how to hook into the Episerver pipeline to prevent this from happening.

The Episerver Pipeline

I 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 exposed to allow us as developers access to run custom code in key stages in the publishing pipeline to add our own business logic. In the example I'm covering in this tutorial, preventing page deletion, the event that we care about hooking into is the 'DeletingContent' event. In order to get the event to trigger successfully, we will create a custom InitializableModule module, that will get called whenever any page is attempted to be deleted from Episerver. Within that method, we will then need to check for the page type we want to prevent from deleting.

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 being permanently deleted (hard delete) from the trash can. This method will allow people to move things to the trash, but, will stop the item being removed from the trash.

The code above details everything that needs to be implemented to provide this functionality. First, in the 'Initialize' method, we create an instance to the IContentEvents API. This is the APPI that will give us access to the Episerver pipeline.

The next step is to create a 'DeletingContent' method that will implement the business logic. We also need to register this method with the IContentEvents instance (as seen in the second line in 'Initialize'). In DeletingContent we use the ContentEventArgs.Content property to get access to the current Episerver item that has been requested to be deleted. We use a standard 'is' operator to check the current page matches the custom type we are trying to prevent from being deleted, by enabling the CancelAction property.

In this example, I'm presenting every page from being deleted. In a real scenario, I would assume you would query the third party to check if the page is being used. Finally, I add a message that will be displayed in the Episerver editor, detailing why the page hasn't saved. This is done using the 'CancelReason ' property.

Preventing A Soft Delete / Moving An Item To The Trash Completely

The second way of preventing a delete is to stop a content editor moving the item to the trash in the first place. As we are not

The code above is pretty similar to the hard delete options, so I won't cover it in as much detail. In this example, instead of hooking into the DeletingContent event, you need to hook into MovingContent. As this method will be called every time any page type is moved, a few additional checks are made.

Within Episerver, the trash can have a content reference Id of 2. So in the moving content code, we not only need to check that the requested page to move is the type we care about, we also need to check that the target isn't in the trash can, this is done with the 'e.TargetLink.ID == 2' bit.

Conclusion

In today's tutorial, we've talked about some different approaches to preventing Episerver pages from being deleted; an example of one such scenario is when you store references to Episerver pages externally from the Episerver editor. If you want to prevent pages from being deleted, you will need to hook into the Episerver's publishing event pipeline, using the IContentEvents API.

After you create an instance to the API, you will need to register an event method. The event method will be triggered when an associated event in Episerver is performed. In the event method, we can get access to useful properties, like Content (to get the current page), CancelAction and CancelReason.