1

The best practice for content editors is to check in Sitecore item after editiing is done.But they often don't follow this practice. So when some other Content editor comes to that item, he manually has to unlock item and Edit it.

So is there a way we can get a list of all such locked items and unlock them programmatically instead of unlocking all them manually?

7

There is a Locking API that you can use.

enter image description here

Now it is up to you how you will process all items and unlock them. You can create a job, SPE (Sitecore Powershell Extensions) script.

| improve this answer | |
2

Here is a good article to unlock bulk items in sitecore using powershell.

What the script does is to specify the path where you want to focus the script to work on the locked items.

$sourcePath = "/sitecore/content/Philippines/Manila" 

function RunScript { $items = Get-ChildItem -Path $sourcePath -Recurse $rootItem = Get-Item -Path $sourcePath $items = $items + $rootItem

    foreach ($item in $items)
    {
        foreach ($version in $item.Versions.GetVersions($true))
        {
            if($version.Locking.IsLocked())
            {
                $version.Editing.BeginEdit();
                $version.Locking.Unlock();
                $version.Editing.EndEdit();
                Write-Host "Item un-locked:" $item.ID $version.Language;
            }
        }
    }
    }

$items = RunScript
| improve this answer | |
1

I got a solution here in the link below

https://sitecorejunkie.com/2013/09/28/periodically-unlock-items-of-idle-users-in-sitecore/

We can create a task and run after every predefined no of hours. e.g 4 hrs in below example

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

Code for UnlockItems class

using System;
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 UnlockItems
{
    private static readonly TimeSpan ElapsedTimeWhenIdle = GetElapsedTimeWhenIdle();

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

        IEnumerable<Item> lockedItems = GetLockedItems(schedule.Database);
        foreach (Item lockedItem in lockedItems)
        {
            UnlockIfApplicable(lockedItem);
        }
    }

    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))
        {
            return;
        }

        Unlock(item);
    }

    private static bool ShouldUnlockItem(Item item)
    {
        Assert.ArgumentNotNull(item, "item");
        if(!item.Locking.IsLocked())
        {
            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");
        try
        {
            string owner = item.Locking.GetOwner();
            item.Editing.BeginEdit();
            item.Locking.Unlock();
            item.Editing.EndEdit();
            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;
    }
}

}

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.