Published: 05 January 2016 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.



Richard Brisley

Lead Server-Side Developer

READ MORE FROM RICHARD BRISLEY

Page Name: {% PageName %}

Page Template: {% PageTemplate %}

CampaignID: {% AgentReferrer.ID %}

CampaignName: {% AgentReferrer.Name %}

CampaignPhone: {% AgentReferrer.Phone %}

Item Location: {% PageLocation %}

Search Session Exists: False