Sitecore Powershell Scripted Task

Sometimes as a Sitecore developer, it is necessary to automate common tasks to make your content editors happy. In this blog post, we will examine how we can use Sitecore Powershell Scripted Task Schedule. For demonstration purposes, we will create a scheduled tasks to execute a Sitecore Powershell script that unlock locked items.

Before we start, make sure that Sitecore Powershell Extension (SPE) is installed. You can download the Sitecore Powershell Extension in Sitecore MarketPlace.

Once Sitecore Powershell Extension is installed, follow the steps below:

Creating the script

  1. Navigate to /Sitecore/System/Modules/PowerShell/Script Library.
  2. Right click the Script Library then hover to the Insert menu. Select the PowerShell Script Module Folder from the list and name the folder MyProject.Sitecore Powershell Script Module location
  3. Right click the MyProject then select the Insert menu. Click the PowerShell Script Module from the list and name the module MyModule.
  4. Open the Powershell ISE found in Development Tools in your Desktop View.
  5. Create a new Powershell script then paste the below code. Modify the value of the sourcepath.
  6. Save the file under the node MyModule inside the node MyProject. Name the script Unlock Locked Items.
$sourcePath = "/sitecore/content/Philippines/Manila"
function RunScript
{
    $items = Get-ChildItem -Path $sourcePath -Recurse
    $rootItem = Get-Item -Path $sourcePath
    $items = $items + $rootItem

    foreach ($item in $items)
    {
        foreach ($version in $item.Versions.GetVersions($true))
        {
            if($version.Locking.IsLocked())
            {
                $version.Editing.BeginEdit();
                $version.Locking.Unlock();
                $version.Editing.EndEdit();
                Write-Host "Item un-locked:" $item.ID $version.Language;
            }
        }
    }
}

$items = RunScript

Credit to Pranay Deegoju for the above script.

Creating the Sitecore Powershell Scripted Task Schedule

You can create the Powershell Scripted Task Schedule under the node schedules in sitecore/system/tasks.

  1. Right click the node schedules under the sitecore/system/tasks then select the Insert menu then click Powershell Scripted Task Schedule. A dialog will popup similar to what is shown below. Sitecore Scripted Schedule Dialog
  2. Type Unlock locked items as name of the task schedule.
  3. Under the Script to Run, Select the script that we created earlier.
  4. Click the create button.
  5. The Schedule for Task dialog displays. Modify the start date and start time. In this example, I set the start date to 3/13/2020 and the start time to 12:00 AM. Click change button once editing is done.Schedule for tasks dialog
  6. To verify if the script runs, navigate to the unlock locked item tasks. Under the data section, there is a field called Last Run. The date indicated there is the date when the script is last executed.Check the last run of the scheduled tasks

Please leave your comments below. Salamat Po (Thank you)!

Security Restriction

If you encounter security restriction after executing the above code. You may need to implement UserSwitcher. To know how you can implement it, please visit my other post.

This Post Has One Comment

  1. Hanz Enriquez

    Managing Locked items is always a headache for every content editor. Most of the times they complained about items that are still locked under their account even if they unlock the actual page. They will just realized that the related items are still locked under their account.

    This article will most likely be very helpful to all developers to manage a lot of locked items. Just a few suggestions though, maybe we can add an option from the Ribbon that will allow the user to set the scheduled date of unlocking the items and maybe an field that will let the user to set the path of the affected items.

    Great Article! Keep it up!

Leave a Reply

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