Sitecore use item:deleting event to avoid deletion of items

The Sitecore extendable model allows you to build business rules into your Sitecore solution. This is an example where I disallow deletion of certain items when a certain business rule is true.

This pattern could be implemented using a field or template validator, or by using the Sitecore workflow,  but it would require you to remember to add the validator to the correct template, and also requires that no one tampers with your template settings.

Sitecore have an item:deleting event that is fired before an item is deleted. The arguments include a Result.Cancel property that can be set to false, thus stopping the deletion of your item. So the implementation looks like this:

using System;
using Sitecore.Data.Items;
using Sitecore.Events;
using Sitecore.Web.UI.Sheer;

namespace MyProject
  public class OnItemDeletingValidator
    public void OnItemDeleting(object sender, EventArgs args)
      // get the item that is being moved
      Item item = Event.ExtractParameter(args, 0) as Item;
      if (item == null)

      if (item.Database.Name != "master")

      // Only run the rule on a certain template.
      // Replace the "MyTemplate" with the name of your own
      // template
      if (item.TemplateName != "MyTemplate")

      // Check to see if the business rule is true or false
      // If true, do nothing.
      // Implemement your own business rule here
      if (BusinessRule(item))
      // Alert the user and cancel the delete
      SheerResponse.Alert("You cannot delete this item because the business rule is false");
      ((SitecoreEventArgs)args).Result.Cancel = true;

The code above is just a pseudo code example. You need to implement your own business rule, and determine your own template.

Also remember to assign the code to the event using configuration:

<event name="item:deleting">
  <handler type="MyProject.OnItemDeletingValidator, MyProject" method="OnItemDeleting"/>


About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, c#, Sitecore 6, Sitecore 7, Sitecore 8, Sitecore 9 and tagged , , . Bookmark the permalink.

1 Response to Sitecore use item:deleting event to avoid deletion of items

  1. Pingback: Sync Sitecore content to external database | Brian Pedersen's Sitecore and .NET Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.