Home » Customization » Periodically Unlock Items of Idle Users in Sitecore

Periodically Unlock Items of Idle Users in Sitecore

Sitecore Technology MVP 2016
Sitecore MVP 2015
Sitecore MVP 2014

Enter your email address to follow this blog and receive notifications of new posts by email.



In my last post I showed a way to unlock locked items of a user when he/she logs out of Sitecore.

I wrote that article to help out the poster of this thread in one of the forums on SDN.

In response to my reply in that thread — I had linked to my previous post in that reply — John West, Chief Technology Officer of Sitecore, had asked whether we would also want to unlock items of users whose sessions had expired, and another SDN user had alluded to the fact that my solution would not unlock items for users who had closed their browser sessions instead of explicitly logging out of Sitecore.

Immediate after reading these responses, I began thinking about a supplemental solution to unlock items for “idle” users — users who have not triggered any sort of request in Sitecore after a certain amount of time.

I first began tinkering with the idea of using the last activity date/time of the logged in user — this is available as a DateTime in the user’s MembershipUser instance via the LastActivityDate property.

However — after reading this article — I learned this date and time does not mean what I thought it had meant, and decided to search for another way to ascertain whether a user is idle in Sitecore.

After some digging around, I discovered Sitecore.Web.Authentication.DomainAccessGuard.Sessions in Sitecore.Kernel.dll — this appears to be a collection of sessions in Sitecore — and immediately felt elated as if I had just won the lottery. I decided to put it to use in the following class (code in this class will be invoked via a scheduled task in Sitecore):

using System;
using System.Collections.Generic;
using System.Web.Security;

using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Security.Accounts;
using Sitecore.Tasks;
using Sitecore.Web.Authentication;

namespace Sitecore.Sandbox.Tasks
    public class UnlockItemsTask
        private static readonly TimeSpan ElapsedTimeWhenIdle = GetElapsedTimeWhenIdle();

        public void UnlockIdleUserItems(Item[] items, CommandItem command, ScheduleItem schedule)
            if (ElapsedTimeWhenIdle == TimeSpan.Zero)

            IEnumerable<Item> lockedItems = GetLockedItems(schedule.Database);
            foreach (Item lockedItem in lockedItems)
        private static IEnumerable<Item> GetLockedItems(Database database)
            Assert.ArgumentNotNull(database, "database");
            return database.SelectItems("fast://*[@__lock='%owner=%']");

        private void UnlockIfApplicable(Item item)
            Assert.ArgumentNotNull(item, "item");
            if (!ShouldUnlockItem(item))

        private static bool ShouldUnlockItem(Item item)
            Assert.ArgumentNotNull(item, "item");
                return false;

            string owner = item.Locking.GetOwner();
            return !IsUserAdmin(owner) && IsUserIdle(owner);

        private static bool IsUserAdmin(string username)
            Assert.ArgumentNotNullOrEmpty(username, "username");
            User user = User.FromName(username, false);
            Assert.IsNotNull(user, "User must be null due to a wrinkle in the interwebs :-/");
            return user.IsAdministrator;

        private static bool IsUserIdle(string username)
            Assert.ArgumentNotNullOrEmpty(username, "username");
            DomainAccessGuard.Session userSession = DomainAccessGuard.Sessions.Find(session => session.UserName == username);
            if(userSession == null)
                return true;

            return userSession.LastRequest.Add(ElapsedTimeWhenIdle) <= DateTime.Now;

        private void Unlock(Item item)
            Assert.ArgumentNotNull(item, "item");
                string owner = item.Locking.GetOwner();
                Log.Info(string.Format("Unlocked {0} - was locked by {1}", item.Paths.Path, owner), this);
            catch (Exception ex)
                Log.Error(this.ToString(), ex, this);

        private static TimeSpan GetElapsedTimeWhenIdle()
            TimeSpan elapsedTimeWhenIdle;
            if (TimeSpan.TryParse(Settings.GetSetting("UnlockItems.ElapsedTimeWhenIdle"), out elapsedTimeWhenIdle))
                return elapsedTimeWhenIdle;
            return TimeSpan.Zero;

Methods in the class above grab all locked items in Sitecore via a fast query, and unlock them if the users of each are not administrators, and are idle — I determine this from an idle threshold value that is stored in a custom setting (see the patch configuration file below) and the last time the user had made any sort of request in Sitecore via his/her DomainAccessGuard.Session instance from Sitecore.Web.Authentication.DomainAccessGuard.Sessions.

If a DomainAccessGuard.Session instance does not exist for the user — it’s null — this means the user’s session had expired, so we should also unlock the item.

I’ve also included code to log which items have been unlocked by the Unlock method — for auditing purposes — and of course log exceptions if any are encountered — we must do all we can to support our support teams by capturing information in log files :).

I then created a patch configuration file to store our idle threshold value — I’ve used one minute here for testing (I can’t sit around all day waiting for items to unlock ;)):

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <setting name="UnlockItems.ElapsedTimeWhenIdle" value="00:00:01:00" />

I then created a task command for the class above in Sitecore:


I then mapped the task command to a scheduled task (to learn about scheduled tasks, see John West’s blog post where he discusses them):


Let’s light the fuse on this, and see what it does.

I logged into Sitecore using one of my test accounts, and locked some items:


I then logged into Sitecore using a different account in a different browser session, and navigated to one of the locked items:


I then walked away, made a cup of coffee, returned, and saw this:


I opened up my latest Sitecore log, and saw the following:


I do want to caution you from running off with this code, and putting it into your Sitecore instance(s) — it is an all or nothing solution (it will unlock items for all non-adminstrators which might invoke some anger in users, and also defeat the purpose of locking items in the first place), so it’s quite important that a business decision is made before using this solution, or one that is similar in nature.

If you have any thoughts on this, please leave a comment.



  1. pieterbrink123 says:

    And another great solution! Do you intent to release this as a module?

    • Thanks Pieter!

      I wasn’t planning on submitting this to the marketplace — I have mixed feelings over automatically unlocking items.

      However, if there is a demand for it, or if you think it would be useful to the community then I will.


    • I’ve changed my mind after having this conversation with Sitecore MVP Robbert Hock.

      I’m going to submit this to the Sitecore Marketplace soon.


      • dmead says:

        I really like your efforts here and submitting anything like this only helps us all in the Sitecore community. If you have ability to do this, I’d suggest auto-unlocking items that were locked for more than X days (or hours). Folks would then know that their lock is only good for e.g. “up to 5 days” and then will be automatically unlocked. Even better would be an option to automatically email the lock holder ahead of time informing them of the auto-unlock about to occur, so they can check-in and then re-lock if needed (e.g. when 80% of the X days (or hours) have passed by send them an email letting them know of the unlock about to occur). That would be amazing and folks would install it a lot I’d hope.

      • This idea is brilliant!



    • Hi Pieter,

      This is now available on the Sitecore Marketplace as a module named Lock Pick: http://marketplace.sitecore.net/Modules/Lock_Pick.aspx


  2. Jan Bühler says:


    do you persist in your warning about using it? I think you made quite sure that there are no unintentional unlockings. If you changed your opinion, please reflect it in the post 🙂

    Thanks for the module,

  3. Mortaza says:


    You will not be able to get an item if it has restriction like Publishing on it and in that case you need to call Sitecore.Context.Site.DisableFiltering = true; and make sure to set the Sitecore.Context.Site.DisableFiltering to false after you are done.



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.

%d bloggers like this: