Sitecore PowerShell script to remove/update workflows

Pranay
May 16, 2020  ยท  8148 views

This post provides Sitecore PowerShell script to remove or update workflow of all items including all languages and versions under a Sitecore node recursively.

Is this not the script you are looking for? Then check the complete list of Sitecore Powershell scripts

Steps to remove workflow in Sitecore

You may want to remove workflows in all your Sitecore items, removing them manually may not be easy job. Instead you can use a Powershell script to run through all the items in the Sitecore, remove the workflows and publish them in one go.

Note: Updating the workflow is similar to that of removing a workflow. To update the workflow instead of removing, assign the required workflow or workflow state value (ID) instead of assigning an empty value.

Workflow

Steps:

  1. Retrieve all the child items recursively.
  2. For each item, get all the versions for all languages.
  3. Check if the workflow state or workflow has some or a specific value. This helps in checking if the items has a particular workflow state (say Draft) of a particular work flow. This can be extended for checking multiple states and multiple workflows.
  4. If the criteria matches start editing the item, remove/update the workflow, state or both.
  5. Optionally you can even publish the item version.

The script below write the item details on to the console and also returns them in a list view which can be exported to a csv file or excelsheet.

Sitecore Powershell script

$props = @{
   InfoTitle = "Remove workflows"
    PageSize = 10000000
}

Write-Host "Starting work in the context of the 'master' database, under /Home item."
Set-Location -Path "master:/sitecore/content/MySite1/Home"

# Set up variables
$workflowState1 = "{xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" #ID of the workflow states. 
$workflowState2 = "{yyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy}"
$workflowState3 = "{zzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz}"

function Remove-Workflows {

    # Logic
    foreach ($item in Get-ChildItem . -Recurse)
    {
        foreach ($version in $item.Versions.GetVersions($true))
        {
            if (($version.Fields["__Workflow state"].Value -eq $workflowState1) -or ($version.Fields["__Workflow state"].Value -eq $workflowState2) -or ($version.Fields["__Workflow state"].Value -eq $workflowState3))
            {
                $version.Editing.BeginEdit();
                $version.Fields["__Workflow state"].Value = ""
                $version.Editing.EndEdit();
                Publish-Item $version -Target "web" -PublishMode SingleItem -Language $version.Language 
            
                Write-Host $version.ID " - " $version.Language 
                $version;
            }
            else
            {
                #Write-Host "NOT UPDATED: " $version.ItemPath " - " $version.Language 
            }
        }   
    }

}

$items = Remove-Workflows
$items | Show-ListView @props -Property ItemPath, ID, @{Label="Language"; Expression={$_."Language"}} 
Close-Window

Hope this helps you.

AUTHOR

Pranay

A Software Engineer by profession, a part time blogger and an enthusiast programmer. You can find more about me here.


Post a comment




Thank you! You are now subscribed.

Sign up for our newsletter

Subscribe to receive updates on our latest posts.

Thank you! You are now subscribed.