Finding Locked Items Using the Sitecore Database.

finding-locked-items

We had an issue recently were a number of items were locked in the Sitecore interface. 

So to find out which users we needed to ask I created the SQL below. This will output the items effected by locks and the xml for the lock which shows the username and password.

SELECT i.ID AS ItemID, i.Name AS ItemName, FieldItem.Name AS FieldName, f.Value AS FieldValue,
TemplateItem.ID AS TemplateID, TemplateItem.Name AS TemplateName
FROM dbo.Items i
INNER JOIN dbo.Fields f ON i.ID = f.ItemId
INNER JOIN dbo.Items FieldItem ON FieldItem.ID = f.FieldId
INNER JOIN dbo.Items TemplateItem ON TemplateItem.ID = i.TemplateID
WHERE FieldItem.Name = '__Lock'

You could add i.TemplateID to the where clause to only search a particular type of item. If you want to pull out the username and lock time from the xml you can do that quite easily. This is using simple CHARINDEX and SUBSTRING so nothing heavy going. If you needed to do this on a monster database you might need to use SQL Servers xml passing instead.
SELECT DISTINCT i.ID AS ItemID, i.Name AS ItemName, FieldItem.Name AS FieldName, f.Value AS FieldValue,
TemplateItem.ID AS TemplateID, TemplateItem.Name AS TemplateName,
SUBSTRING(f.Value, CHARINDEX('owner="', f.Value) + 7, CHARINDEX('"', SUBSTRING(f.Value, CHARINDEX('owner="', f.Value) + 7, LEN(f.Value) - CHARINDEX('owner="', f.Value) + 7)) - 1) AS LockUsername,
SUBSTRING(f.Value, CHARINDEX('date="', f.Value) + 7, CHARINDEX('"', SUBSTRING(f.Value, CHARINDEX('date="', f.Value) + 7, LEN(f.Value) - CHARINDEX('date="', f.Value) + 7)) - 1) AS LockDate
FROM dbo.Items i
INNER JOIN dbo.Fields f ON i.ID = f.ItemId
INNER JOIN dbo.Items FieldItem ON FieldItem.ID = f.FieldId
INNER JOIN dbo.Items TemplateItem ON TemplateItem.ID = i.TemplateID
WHERE FieldItem.Name = '__Lock'

To create a quick aggregate of the total locks by username simply do this.
SELECT LockUsername, COUNT(*) AS TotalLocks
FROM
(
SELECT DISTINCT i.ID AS ItemID, i.Name AS ItemName, FieldItem.Name AS FieldName, f.Value AS FieldValue,
TemplateItem.ID AS TemplateID, TemplateItem.Name AS TemplateName,
SUBSTRING(f.Value, CHARINDEX('owner="', f.Value) + 7, CHARINDEX('"', SUBSTRING(f.Value, CHARINDEX('owner="', f.Value) + 7, LEN(f.Value) - CHARINDEX('owner="', f.Value) + 7)) - 1) AS LockUsername,
SUBSTRING(f.Value, CHARINDEX('date="', f.Value) + 7, CHARINDEX('"', SUBSTRING(f.Value, CHARINDEX('date="', f.Value) + 7, LEN(f.Value) - CHARINDEX('date="', f.Value) + 7)) - 1) AS LockDate
FROM dbo.Items i
INNER JOIN dbo.Fields f ON i.ID = f.ItemId
INNER JOIN dbo.Items FieldItem ON FieldItem.ID = f.FieldId
INNER JOIN dbo.Items TemplateItem ON TemplateItem.ID = i.TemplateID
WHERE FieldItem.Name = '__Lock'
) data
GROUP BY LockUsername

I hope this is helpful to someone and prevents them having to write the whole thing from scratch.

 

 

want to speak to one of our experts?

Richard Brisley
Richard Brisley
Tech Lead
Richard is the longest standing member of the Sagittarius team, he works tirelessly to support the development and side-facing team with problem solving and pitches alike. His skills as a .NET programmer and database administrator have been paramount to the success of Sagittarius and our continued success. 

In 2016 and again in 2019 Richard was recognise in the BIMA 100 awards for his outstanding work in Tech, his passion for digital and his contribution to the industry. 

Richard Brisley

Richard Brisley

05 Jan 2016 - 6 minute read
share this

stay in the know, stay ahead.

Get the latest from the agency, including news, events and expert content.
explore services in the article
find out what we can do for you
read some of our case studies