Sitecore disable automatic lock on create items

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 config property is true.

Item is always locked on create

Item is always locked on create

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>
      <pipelines>
        <addFromTemplate>
          <processor type="MyCode.UnlockItem, MyDll"/>
        </addFromTemplate>
      </pipelines>
    </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:

Advertisements

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, c#, General .NET, Sitecore 9 and tagged , , , . Bookmark the permalink.

5 Responses to Sitecore disable automatic lock on create items

  1. Andy Burns says:

    Are you sure? I just tried this on a Sitecore 9.0 Update 2 instance, and my newly created item was NOT locked. Or is this behaviour different for Admin users?

  2. briancaos says:

    Yes, I should have mentioned this. For administrators, items will not be locked. This is probably why nobody have mentioned this new “feature” before, as everyone runs Sitecore in admin mode :)

  3. Marie Høg says:

    Do you have any idea what to do about branches? :)

  4. briancaos says:

    Sorry, no. Maybe Sitecore support have a solution, but as far as I can see, Sitecore lacks a pipeline to modify branches.

  5. Marie Høg says:

    Yes that was also my finding. Writing to support at this instant.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.