contact@skillcore.net

Sitecore Unlock Item Command

Posted 31 May 2018 by Marek Musielak

sitecore unlock item

This article explains how to add Unlock Item command button in Sitecore Content Editor.

TL;DR; Download Sitecore content package of Unlock Item Button and install it on you Sitecore CM server. Every time an item is locked, content author will see Unlock Item button in Sitecore Content Editor ribbon in Review > Locks section:

sitecore unlock item button

Why do I need Unlock Item button?

Sometimes content authors forget to unlock an item after editing its content. Other content authors are not allowed to unlock and edit those items (unless they have administrator privileges).

How can I install Unlock Item button?

Just download Sitecore content package of Unlock Item Button and install it on you Sitecore CM server or alternatively copy the code from here (thanks to John Rappel for his comment about adding item.Access.CanWrite() check):

using Sitecore.Shell.Framework.Commands;

namespace Skillcore.Tools
{
    public class UnlockItem : Command
    {
        public override void Execute(CommandContext context)
        {
            if (context.Items.Length != 1)
                return;

            var item = context.Items[0];
            if (item.Locking.IsLocked() && item.Access.CanWrite())
            {
                using (new Sitecore.SecurityModel.SecurityDisabler())
                {
                    item.Locking.Unlock();
                }
            }
        }

        public override CommandState QueryState(CommandContext context)
        {
            if (context.Items.Length != 1)
                return CommandState.Hidden;

            var item = context.Items[0];

            if (!item.Locking.IsLocked() || !item.Access.CanWrite())
                return CommandState.Hidden;

            return base.QueryState(context);
        }
    }
}

Then use the following config to register the new command:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <commands>
      <command name="contenteditor:unlockitem" type="Skillcore.Tools.UnlockItem, Skillcore.Tools" />
    </commands>
  </sitecore>
</configuration>

And add a new button in core database under: /sitecore/content/Applications/Content Editor/Ribbons/Chunks/Locks item:

sitecore unlock item button core database

Simply as that - nothing more is necessary. Now your content authors can do their job without wasting their (and your) time. Thank you for reading. You can find more Sitecore related articles here.