Track when user clicks on Lock & Edit item

It can be a case when you need to perform some action right after user has clicked ‘Edit’ button.
lock-edit

To do that you need firstly patch “item:saved” event by adding your own custom handler:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <events>
      <event name="item:saved">
        <handler type="Demo.Data.Items.ItemEventHandler, Demo" method="OnItemSaved"/>
      </event>
    </events>
  </sitecore>
</configuration>

Secondly, implement ItemEventHandler class and OnItemSaved method:

namespace Demo.Data.Items
{
    using Sitecore.Data.Items;
    using Sitecore.Events;
    using System;

    public class ItemEventHandler
    {
        protected void OnItemSaved(object sender, EventArgs args)
        {
            if (args == null) { return; }

            var sitecoreEventArgs = args as SitecoreEventArgs;
            if (sitecoreEventArgs == null) { return; }

            if (sitecoreEventArgs.Parameters.Length < 2) { return; }

            var item = sitecoreEventArgs.Parameters[0] as Item;
            if (item == null) { return; }

            if (!this.ShouldItemBeProcessed(item)) { return; }

            var itemChanges = sitecoreEventArgs.Parameters[1] as ItemChanges;
            if (itemChanges == null) { return; }

            if (itemChanges.FieldChanges.Contains(Sitecore.FieldIDs.Lock))
            {
                var fieldChange = itemChanges.FieldChanges[Sitecore.FieldIDs.Lock];
                if (fieldChange.Value == "<r />") { return; }

                this.DoWhatYouNeed();
            }
        }

        protected bool ShouldItemBeProcessed(Item item)
        {
            // TODO implement necessary check
            return true;
        }

        protected void DoWhatYouNeed()
        {
            // TODO
        }
    }
}

Several words about how and why it works: when user clicks ‘Edit’ button Sitecore writes some value to the Lock field:
lock-edit-1

Under the hood Sitecore fires “item:saved” event and calls OnItemSaved method of each handler. This method has two parameters and the second one is args of EventArgs type. It can be cast to SitecoreEventArgs type which has Parameters property. The second item within Parameters property is instance of ItemChanges type. ItemChanges has FieldChanges property contains сhanges of saved item. Each of change is instance of FieldChange type. If value of Value propetry (sorry for the tautology) of fieldChange instance is

<r />

it means that user has clicked ‘Check In’ button, in other case it means that user has clicked ‘Edit’ button.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s