How To Stop A Page Being Deleted In Umbraco

Providing good validation in the Umbraco back-end is an often overlooked but key component when implementing a successful content editor experience. One way to do this is preventing content editors from deleting things they shouldn’t. One example of this would be the homepage. If someone accidently deleted that, the whole site would break. Luckily, Umbraco has some useful hooks that allow us, as developers, to write code to prevent this. In today’s tutorial, I will discuss the Umbraco publishing events pipeline.

Umbraco Events Pipeline

When a content editor creates or deletes a page in Umbraco, it follows the Umbraco events pipeline. When you try to delete content, a delete event is raised, when you try and save a page, the save event is triggered.

In order for a developer to hook into these events, we must use the Umbraco ApplicationEventHandler. Hooking into ApplicationEventHandler is easy, as it’s a base class so all you need to do is inherit from it and override the methods of the events you want to customise.

So, if we want to add some validation to prevent certain document types from being deleted, all you need to do is hook into the Trashing and UnPublishing events

The Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Publishing;
using Umbraco.Core.Services;
namespace JonDJones.BusinessLayer.Events
{
public class UmbracoEventsHandler : ApplicationEventHandler
{
public UmbracoEventsHandler()
{
ContentService.Trashing += this.ContentServiceTrashing;
ContentService.UnPublishing += this.ContentServiceUnPublishing;
}
private IEnumerable<string> ContentTypes
{
get
{
return new[] { "Homepage" };
}
}
private void ContentServiceTrashing(IContentService sender, MoveEventArgs<IContent> e)
{
var aliasName = e.Entity.ContentType.Alias;
if (ContentTypes.Contains(aliasName))
{
e.Cancel = true;
e.Messages.Add(CreateWarningMessage(aliasName));
}
}
private void ContentServiceUnPublishing(IPublishingStrategy sender, PublishEventArgs<IContent> e)
{
foreach (var item in e.PublishedEntities)
{
if (ContentTypes.Contains(item.ContentType.Alias))
{
e.Cancel = true;
e.Messages = CreateWarningMessage(item.ContentType.Alias);
break;
}
}
}
private static EventMessage CreateWarningMessage(string aliasName)
{
var eventMessage = new EventMessage("Warning", string.Format("Can not delete {0}", aliasName));
return eventMessage;
}
}
}

Ok, so let’s talk through what the code is doing. First, we inherit from ApplicationEventHandler. We create a constructor and register two events Trashing and UnPublishing and create two methods that override.

Both methods do pretty much the same thing. They look in the event args for the alias of the document type being requested to be deleted, if the type is of type ‘homepage’ then the request is cancelled and an event message is generated and returned to the user. This will pop up in the back-end so the user knows why the page wasn’t deleted.

After you compile the code and run the back-end, provided your home pages alias is ‘homepage’ then you will now be unable to delete it.

Conclusion

In today’s post, we’ve discussed the Umbraco events handling for the page publishing pipeline. We can hook into different events and add custom logic, by implementing the ApplicationEventHandler. In this class, we register events we want to customise and override the base class methods. You interrogate the event arguments to check the request matches your criteria, then you cancel the request and set a message to return to the content editor describing why the request was cancelled.

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

2 replies
  1. Jonathan Roberts
    Jonathan Roberts says:

    Hi,
    Is it possible to disable the Save and publish button once a page has been sent to translation or approval?
    Thanks
    Jon

    Reply

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 *