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) return; if (item.Database.Name != "master") return; // Only run the rule on a certain template. // Replace the "MyTemplate" with the name of your own // template if (item.TemplateName != "MyTemplate") return; // Check to see if the business rule is true or false // If true, do nothing. // Implemement your own business rule here if (BusinessRule(item)) return; // 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"/> </event>
MORE TO READ:
- Validate Your Understanding of Sitecore Field Validation by Sitecore Spark
- Sitecore Events from Sitecore Community Documentation
- Adding workflow for item deletion from StackExchange