Unlock sitecore items

Some of my users would like to be able to unlock other users items. This is impossible unless you are an administrator. So I created a new button for the users. This is how it’s done.

First you have to place the button in Sitecore. Open the core database and find the “Workflow” ribbon chunk:

/sitecore/content/Applications/Content Editor/Ribbons/Chunks/Workflow

Then create a new “Small Button“. Name the button anything you wish. Write a command name in the “Click” field, for example “mycommand:unlockitem“. Also, remember to apply a header, tooltip and an icon, and remember to translate the header and tooltip :-)

When clicking the button, Sitecore executes the “mycommand:unlockitem” command. The connection between the command and a C# dll is created in the /App_Config/commands.config XML file. You can either extend the commands.config file directly, or create your own .config file and place the file in the /App_Config/Include folder. Sitecore will automatically merge anything in the /App_Config/Include folder into it’s configurations:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:x="http://www.sitecore.net/xmlconfig/">
      <command name="mycommand:unlockitem" type="PT.Locks.Commands.UnlockItem, PT.Locks" />   

The unlock code is pretty simple. You inherit from the Sitecore.Shell.Framework.Commands.Command class. You need to overwrite 2 functions. Execute() contains the code that is executed when the button is clicked. QueryState() determines if the button should be active (or visible) at all.

using System;
using Sitecore;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Shell.Framework.Commands;

namespace PT.Locks.Commands
  public class UnlockItem : Command
    /// <summary>
    /// Executes the command in the specified context.
    /// </summary>
    /// <param name="context">The context.</param>
    public override void Execute(CommandContext context)
      Item item = context.Items[0];
      if (item.Access.CanWriteLanguage() && item.Locking.IsLocked())
        Log.Audit(this, "Check in: {0}", new[] {AuditFormatter.FormatItem(item)});
        Context.ClientPage.SendMessage(this, "item:checkedin");

    /// <summary>
    /// Queries the state of the command.
    /// </summary>
    /// <param name="context">The context.</param>
    /// <returns>The state of the command.</returns>
    public override CommandState QueryState(CommandContext context)
      Item item = context.Items[0];
      if (item.Access.CanWriteLanguage() && item.Locking.IsLocked() && !item.Locking.HasLock() && !Context.IsAdministrator)
        return CommandState.Enabled;
      return CommandState.Hidden;

This command hides the button if you are an administrator (admins have an unlock button already) or if the item is not locked (obviously), or if the item is locked by you (you also have an unlock button to unlock your own items).

You can also apply security to the button so only specific users can unlock other users items.

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, c#, Sitecore 6 and tagged , , , . Bookmark the permalink.

15 Responses to Unlock sitecore items

  1. With Security, this button makes perfect sense: Obviously you would not want anybody in a large enterprise to start unlocking each others items.

    Alternativly, you could make this a “request unlock” button, which ships an email to the owner.

  2. Pingback: XAML (SheerUI) dialog in Sitecore 6.3.x « Brian Pedersen’s Sitecore and .NET Blog

  3. Pingback: Deleting items to recycle bin in Sitecore « Brian Pedersen’s Sitecore and .NET Blog

  4. Craig says:

    Great post! The only catch is that the users have to be setup with local administration role for the site.

  5. Malu says:

    I implemented this and it works great. But the only issue i see is for the Media Items. The users are not able to unlock media. Our media is not versionable. Any ideas why this is happening?

  6. briancaos says:

    When you create a new nedia item you choose wether the item is versionable or not. As per default, media items are not versionable. You need to use the advanced uploader to see the checkmark where you can choose the versionable items.

  7. Malu says:

    Thanks for your response. Ok I tried it but that did not solve my problem. Still not able to unlock the Media items locked by other users. Did anyone else experience the same situation?

  8. Corey Burnett says:

    Yes, we are experiencing the same problem with media items. The users are not able to unlock media items.

  9. Michael Metdepenningen says:

    Using Sitecore 6.5 rev. 120427 I had to use the SecurityDisabler in order for the unlocking to work.

  10. Silvio Mascioli says:

    Do you know why it doesn’t work on Sitecore 6.6?

  11. briancaos says:

    You might have to use thre SecurityDisabler() in order to unlock Sitecore items from version 6.5 and onward.

    using (new SecurityDisabler())

  12. Silvio Mascioli says:

    You were right but i have made another mistake. Now everything works properly!! Thank you very much!!!!

  13. Pingback: Force an unlock on a Sitecore item | Cortex Exodus

  14. cortexodus says:

    Brian, this gave me a great start. I took your post and ran with it.

  15. Pingback: Which of my old Sitecore posts are still valid in Sitecore 9? | Brian Pedersen's Sitecore and .NET Blog

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.