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.
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 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:
item:renamed
event, but that occurs only after the rename is processed. To cancel the rename from happening, we can use the uiRenameItem
event.Below are some of the UI events that are available in Sitecore:
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();
}
}
}
<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>
Happy Sitecoring!