Sitecore updates the item statistics (Updated and Updated by fields) on each lock or unlock operation on an item. Sometimes this is misleading for content authors.
When this is misleading
- When a user is just locking an item and not done any modifications in it but this creates dilemma for other users that the user already had some changes on that item or not. In our multisite environment where hundreds of users work on a single Sitecore instance, this confusion occurs frequently.
- Even some content authors demand to auto-unlock their items after publishing without updating modified Date time.
How we can achieve this?
We will see how we can lock or unlock items without modifying item statistics.Approach#1
The best way to achieve it is using Item's RuntimeSettings itself.public void LockItem(Item item) { if (!item.Locking.IsLocked()) { item.RuntimeSettings.ReadOnlyStatistics = true; item.Locking.Lock(); item.RuntimeSettings.ReadOnlyStatistics = false; } } public void UnlockItem(Item item) { if (item.Locking.IsLocked()) { item.RuntimeSettings.ReadOnlyStatistics = true; item.Locking.Unlock(); item.RuntimeSettings.ReadOnlyStatistics = false; } }Here, item.RuntimeSettings.ReadOnlyStatistics = true; will not update statistics for current context of item. So, while locking or unlocking, it will not update statistics.
Approach#2
Another simple way is to update the __lock field though APIs with updateStatistics to set as false as below. I personally do not recommend this approach, but still works great in many cases where above approach does not work.public void LockItem(Item item) { if (!item.Locking.IsLocked()) { using (new EditContext(item, false, false)) { item["__lock"] = "<r owner=\"" + Context.User.Name + "\" date=\"" + DateTime.Now.ToString("yyyyMMddTHHmmss") + "\" />"; } } } public void UnlockItem(Item item) { if (item.Locking.IsLocked()) { using (new EditContext(item, false, false)) { item["__lock"] = "<r />"; } } }Happy to see it works and happy to see our content authors happy!
No comments:
Post a Comment