How To Prevent A Page Being Deleted in Episerver

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, serialise 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. In Episerver when you try and delete a page, Episerver will prevent you from deleting the page if it is referenced by one or more pages or blocks within your website. 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.

[InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class EventsInitialization : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            var events = ServiceLocator.Current.GetInstance<IContentEvents>();
            events.DeletingContent += DeletingContent;
        }
        public void Preload(string[] parameters)
        {
        }
        public void Uninitialize(InitializationEngine context)
        {
        }
        private void DeletingContent(object sender, ContentEventArgs e)
        {
            if (e.Content is PageType)
            {
                e.CancelAction = true;
                e.CancelReason = "Not Allowed To Delete PageType";
            }
        }
    }

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 simple tutorial, I’m preventing all instances of the page type from being deleted. In a real-world example, 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

[InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class EventsInitialization : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            var events = ServiceLocator.Current.GetInstance<IContentEvents>();
            events.MovingContent += MovingContent ;
        }
        public void Preload(string[] parameters)
        {
        }
        public void Uninitialize(InitializationEngine context)
        {
        }
        private void MovingContent (object sender, ContentEventArgs e)
        {
            if (e.Content is PageType && e.TargetLink.ID == 2)
            {
                e.CancelAction = true;
                e.CancelReason = "Not Allowed To Delete PageType";
            }
        }
    }

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. In 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 Episervers 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.

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

0 replies

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 *