Implementing Pre-Delete Validation in Sitecore Content Editor

 In Sitecore, it’s often necessary to ensure certain conditions are met before allowing the deletion of an item. For example, you may want to prevent the deletion of items that are still published. In this guide, I’ll show you how to add a validation check before deleting an item in Sitecore Content Editor.

Step 1: Creating the Configuration File

First, we must create a configuration file that integrates our custom processor into the Sitecore delete item pipeline. This processor will perform validation checks before allowing an item to be deleted.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
<sitecore>
<processors>
<uiDeleteItems>
<processor patch:before="*[@method='CheckPermissions' and @type='Sitecore.Shell.Framework.Pipelines.DeleteItems,Sitecore.Kernel']" mode="on" type="TL.Foundation.Demo.Pipelines.CustomDeleteHandler, TL.Foundation.Demo" method="OnItemDeleting" />
</uiDeleteItems>
</processors>
</sitecore>
</configuration>

Explanation:

  • The configuration element defines the structure of the configuration file.
  • The processors element contains pipeline processors for various actions in Sitecore.
  • The uiDeleteItems element specifies the pipeline for deleting items in the UI.
  • The processor element adds our custom processor (CustomDeleteHandler) before the existing CheckPermissions processor.

This configuration ensures that our custom delete handler will run before Sitecore’s default CheckPermissions processor, allowing us to perform our validation checks first.


Step 2: Creating the Custom Delete Handler

Next, we create a custom delete handler class. This class will contain the logic to check specific conditions before allowing the deletion of an item.

In this handler, you will write the logic to check if an item meets specific conditions (like being published) before allowing it to be deleted.

using Sitecore;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.SecurityModel;
using Sitecore.Text;
using Sitecore.Web.UI.Sheer;
using System;
using System.Collections.Generic;
namespace TL.Foundation.Demo.Pipelines
{
public class CustomDeleteHandler
{
public void OnItemDeleting(ClientPipelineArgs args)
{
try
{
// To get List of items
var items = GetItems(args);
//Add your validation condition condition
/* To Abort pipeline you can use below code
SheerResponse.Alert("Please unpublish {0} before deletion.", item.Name);
args.AbortPipeline();
*/
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error("Error while custom delete validation" + ex.Message, this);
}
}
private static List<Item> GetItems(ClientPipelineArgs args)
{
Assert.ArgumentNotNull((object)args, nameof(args));
Database database = Factory.GetDatabase(args.Parameters["database"]);
List<Item> result = new List<Item>();
if (database != null)
{
foreach (string path in new ListString(args.Parameters["items"], '|'))
{
Item obj = database.GetItem(path, Language.Parse(args.Parameters["language"]));
if (obj != null)
result.Add(obj);
}
}
return Assert.ResultNotNull<List<Item>>(result);
}
}
}

Explanation:

  1. Configuration File: We set up a custom processor in the Sitecore pipeline that runs before CheckPermissions.
  2. Custom Delete Handler Class: The OnItemDeleting method will retrieve items to be deleted and check if they meet the conditions you’ve set. If they do, an alert message will be displayed, and the deletion process will be aborted.

Conclusion

By following these steps, you can ensure that items are not deleted if certain conditions are met, adding an extra layer of protection to your Sitecore content management process. This method helps prevent accidental deletions and ensures a smoother content management experience.

Feel free to customize the validation logic based on your specific requirements. Happy coding!

0 Comments