Executing a mass check-in of locked items in Sitecore

Recently I was tasked with finding a way to check-in all locked items in a client’s Sitecore environment. Since Sitecore allows you to lock items for editing to prevent concurrent modifications, and some users never removed their lock, we had a frequent problem with items that were locked by users that did not even work there anymore.

The method of accomplishing this was pretty clear from the get-go. It was time for another Sitecore PowerShell Extensions script!

Creating our Script

I knew PowerShell was the answer, but I wasn’t too well versed with it yet. Sitecore PowerShell Extensions (SPE) is pretty powerful though, since you can access the Sitecore item API through it just as you would through code, so I knew it wouldn’t be that hard once I got through the necessary hiccups.

The logic:

  • Start in the context of the master database.
  • Loop through every item in the content tree, recursively.
  • Then, loop through every version of the current item, since it’s possible that only some of the versions of an item will be locked.
  • Check if the item is locked. If so, open it for editing and unlock it. Then close it for editing.

The script:

<# .SYNOPSIS Check in all locked items in master. .BY Paul Aldrich .DATE 5/3/2017 .UPDATE LOG #>
Write-Host "Starting work in the context of the 'master' database."
Set-Location -Path master:\content
$itemsProcessed = 0;
$itemsUnlocked = 0;
foreach ($item in Get-ChildItem . -Recurse)
{
   foreach ($version in $item.Versions.GetVersions())
   {
      $itemsProcessed = $itemsProcessed + 1;

      <# For any item in the content node, check if locked. If so, check it in. #>
      if ($version.Locking.IsLocked())
      {
         Write-Host "Unlocking item"
         $version.Editing.BeginEdit();
         $version.Locking.Unlock();
         $version.Editing.EndEdit();
         $itemsUnlocked = $itemsUnlocked + 1;
      }
   }
}
Write-Host "The script has completed."
Write-Host ("{0} items have been processed." -f $itemsProcessed)
Write-Host ("{0} items were locked and are now checked in." -f $itemsUnlocked)

The script should be pretty straightforward, but let me know if you have any questions in the comments.

Completion

This was a really simple, yet powerful script to make, and the best part is its reusability. Don’t forget, you can check your work by running the Locked Items script that comes with SPE and search for locked items by user.