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/"> <sitecore> <commands> <command name="mycommand:unlockitem" type="PT.Locks.Commands.UnlockItem, PT.Locks" /> </commands> </sitecore> </configuration>
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 { [Serializable] 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)}); item.Editing.BeginEdit(); item.Locking.Unlock(); item.Editing.EndEdit(); 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.
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.
LikeLike
Pingback: XAML (SheerUI) dialog in Sitecore 6.3.x « Brian Pedersen’s Sitecore and .NET Blog
Pingback: Deleting items to recycle bin in Sitecore « Brian Pedersen’s Sitecore and .NET Blog
Great post! The only catch is that the users have to be setup with local administration role for the site.
LikeLike
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?
LikeLike
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.
LikeLike
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?
LikeLike
Yes, we are experiencing the same problem with media items. The users are not able to unlock media items.
LikeLike
Using Sitecore 6.5 rev. 120427 I had to use the SecurityDisabler in order for the unlocking to work.
LikeLike
Do you know why it doesn’t work on Sitecore 6.6?
LikeLike
You might have to use thre SecurityDisabler() in order to unlock Sitecore items from version 6.5 and onward.
using (new SecurityDisabler())
{
item.Editing.BeginEdit();
item.Locking.Unlock();
item.Editing.EndEdit();
}
LikeLike
You were right but i have made another mistake. Now everything works properly!! Thank you very much!!!!
LikeLike
Pingback: Force an unlock on a Sitecore item | Cortex Exodus
Brian, this gave me a great start. I took your post and ran with it.
http://cortexodus.wordpress.com/2013/05/31/this-is-fine/
LikeLike
Pingback: Which of my old Sitecore posts are still valid in Sitecore 9? | Brian Pedersen's Sitecore and .NET Blog