Quantcast
Channel: Brian Pedersen's Sitecore and .NET Blog » Command
Viewing all articles
Browse latest Browse all 5

Unlock sitecore items

$
0
0

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.



Viewing all articles
Browse latest Browse all 5

Trending Articles