From Sitecore 9, Sitecore changed the default behavior when creating items in the content editor. As per default, any item is locked when created, and will only be unlocked if the user unlocks it, or the user saves the item, and the AutomaticUnlockOnSaved confi property is true.
Unfortunately, Sitecore forgot to include a DoNotLockOnCreate property, so it is impossible to get rid of this feature – unless we do some coding.
This piece of code is a collaboration between Sitecore Support and myself, and will work ONLY WHEN CREATING ITEMS FROM TEMPLATES. Items created based on a branch will stay locked.
STEP 1: EXTEND THE addFromTemplate pipeline
The addFromTemplate pipeline runs every time an item is created. We add a new processor that will create the new item, and then call the item:checkin command to unlock the item.
using Sitecore; using Sitecore.Diagnostics; using Sitecore.Pipelines.ItemProvider.AddFromTemplate; using System; namespace MyCode { public class UnlockItem : AddFromTemplateProcessor { public override void Process(AddFromTemplateArgs args) { Assert.IsNotNull(args.FallbackProvider, "FallbackProvider is null"); try { var item = args.FallbackProvider.AddFromTemplate(args.ItemName, args.TemplateId, args.Destination, args.NewId); if (item == null) return; Context.ClientPage.SendMessage(this, "item:checkin"); args.ProcessorItem = args.Result = item; } catch (Exception ex) { Log.Error(this.GetType() + " failed. Removing item " + args.NewId, ex, this); var item = args.Destination.Database.GetItem(args.NewId); item?.Delete(); args.AbortPipeline(); } } } }
STEP 2: ADD THE PROCESSOR TO THE addFromTemplate pipeline
<sitecore> <pipelines> <group> <addFromTemplate> <processor type="MyCode.UnlockItem, MyDll"/> </addFromTemplate> </group> </pipelines> </sitecore>
The code will now run every time you create a new item, unlocking the item immediately after. As stated before, this hack will only work when creating new items based on templates. Items created from a branch will keep a lock on the top branch item.
MORE TO READ:
- Friday Sitecore Best Practice: Automatically Unlock Items to Avoid Locking Issues by Vasiliy Fomichev
- Using Sitecore’s New AddFromTemplate Item Provider Pipeline by Zachary Kniebel
- Sitecore Commands: A Developer’s Friend by Steve VandenBush