All Articles

Sitecore - Custom UI Event Handlers

In this article, I will be covering on what UI events are, and how we can create custom event handlers for various UI events in Sitecore.

Sitecore UI Event Handlers

UI Event Handlers are pipelines which are triggered when events such as Save, Rename, Copy, Move, etc. occur on the content editor. As these are pipelines, we can customize the processors that run when the event occurs.

Sitecore Item Event v/s UI Events

Sitecore offers several item events such as item:saved, item:saving, item:moved, item:renamed etc. We can write custom handlers for these events already. However, UI events and Item events have the following differences:

  • UI Events are triggered only over the actions that occur on the Content Editor, and Experience Editor. Whereas, the Item events are triggered even if the event occurs through custom code, Sitecore Powershell, Package Installation, etc.
  • UI Events are easier for interactive configuration, and to cancel the event from processing. For e.g. We do have an item:renamed event, but that occurs only after the rename is processed. To cancel the rename from happening, we can use the uiRenameItem event.

UI Events in Sitecore

Below are some of the UI events that are available in Sitecore:

  • uiAddFromTemplate - Occurs when a user tries to add an item.
  • saveUI - Occurs when a user tries to save an item.
  • uiCopyItems - Triggered when a user initiates a copy action on items.
  • uiMoveItems - Triggered when a user initiates a move action on items.
  • uiDragItem - Occurs when a use drags an item to a different location in the content tree.
  • uiRenameItem - Occurs when the user tries to rename an item.

Creating a Custom UI Event Handler

  1. Create a class with a method that accepts arguments of type ClientPipelineArgs.
    namespace Foundation.Sitecore.EventHandlers
    {
        public class UiItemsEventHandler
        {
            public virtual void CheckItemForIssues(ClientPipelineArgs args)
            {
                Assert.ArgumentNotNull(args, nameof(args));
                if (!SheerResponse.CheckModified())
                    return;
                Item item = GetItem(args);
                if (item == null)
                {
                    HandleItemNotFound(args);
                }
    	    bool myCondition = false; // Check your condition here
                if(myCondition)
                {
                    // Perform your logic here, use args.AbortPipeline() to cancel operation
                }
            }
    	// This method will need to change depending on the way the item(s) are passed in the args
    	private Item GetItem(ClientPipelineArgs args)
            {
                Assert.ArgumentNotNull(args, nameof(args));
                Database database = Factory.GetDatabase(args.Parameters["database"]);
                Assert.IsNotNull(database, typeof(Database), "Database: {0}", args.Parameters["database"]);
                Language contextLanguage = Language.Parse(args.Parameters["language"]);
                return !string.IsNullOrEmpty(args.Parameters["id"])
                    ? database.GetItem(args.Parameters["id"], contextLanguage) : null;
            }
    	private void HandleItemNotFound(ClientPipelineArgs args)
            {
                Assert.ArgumentNotNull(args, nameof(args));
                SheerResponse.Alert("Item not found.");
                args.AbortPipeline();
            }
        }
    }
  2. Add a patch file to your configuration to the appropriate event as below:
    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
     <sitecore>
      <processors>
       <uiRenameItem>
        <processor patch:after="*[@method='CheckPermissions']" type="Foundation.Sitecore.EventHandlers, Foundation.Sitecore" method="CheckItemForIssues">
        </processor>
       </uiRenameItem>
      </processors>
     </sitecore>
    </configuration>
  3. Publish to your Sitecore instance and test your change.

Happy Sitecoring!

Published Nov 23, 2023

Sitecore MVP Technology 2024-23. Web Developer with rich experience in Sitecore and ASP.NET MVC.