Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Sitecore Powershell script to update the workflow and state on content items which do not have a workflow set on them.
Updates the Workflow and Workflow state fields of content items with the given $workflowID and $workflowStateID values.
This script can be used when existing content is not assigned to a workflow and workflow state while it should be.
This scenario usually occurs when a workflow is assigned to a template but there is already content created based on a previous version of that template (where the workflow was not yet assigned).
The script will first search for the templates that use the $workflowID in the Default workflow field in Standard Values items. This means that the templates which use the workflow must exist on the environment where you are going to run this script.
Then it will update content items which match those templates and have no workflow assigned to them (so it won't overwrite existing workflows/states).
Only the $workflowID and $workflowStateID variables need to be set by the user.
Written by Marc Duiker on May 6th 2016.
#region workflow specific variables
# This is the ID of the workflow that will be set on the content items.
$script:workflowID = "{A5BC37E7-ED96-4C1E-8590-A26E64DB55EA}" #Sample Workflow
# This is the ID of the workflow state that will be set on the content items.
$script:workflowStateID = "{FCA998C5-0CC3-4F91-94D8-0A4E6CAECE88}" #Approved State
function GetTemplatesWhichUseTheWorkflow()
$itemsWithMatchingDefaultWorkflow = Get-Item -Path master: -Query "/sitecore/templates//*[@__Default workflow='$script:workflowID']"
Write-Host "Templates which use workflow" $script:workflowID":"
foreach ($item in $itemsWithMatchingDefaultWorkflow)
# The Default workflow field can only be set for __Standard Value items but checking that nevertheless.
if ($item.Name -eq "__Standard Values")
$script:templateIDsWithDefaultWorkflow.Add($item.TemplateID) > $null # The output of the Add is ignored
Write-Host " -" $item.TemplateName $item.TemplateID
function SetWorkflowAndState([Sitecore.Data.Items.Item]$contentItem)
$contentItem.__Workflow = $script:workflowID
$contentItem."__Workflow state" = $script:workflowStateID
Write-Host " -" $contentItem.Name $contentItem.ID
function ProcessContentItems()
# Update only the content items for the matching templateIDs and an empty Workflow field.
Write-Host "Updating content items to set workflow to" $script:workflowID "and state to" $script:workflowStateID":"
$processedItems = Get-ChildItem -Path master:/content `
-Recurse | `
Where-Object { ($script:templateIDsWithDefaultWorkflow.Contains($_.TemplateID)) -and ($_.__Workflow -eq "") } | `
ForEach-Object { SetWorkflowAndState($_) }
Write-Host "# of processed items:" $script:itemCount
# Declare a new ArrayList to add the IDs of the templates which use the workflow.
# An ArrayList is used instead of the the default PS Array because the latter is immutable and not efficient when working with large arrays.
$script:templateIDsWithDefaultWorkflow = New-Object System.Collections.ArrayList
# Counter to keep track of the updated content items.
$script:itemCount = 0
if ($script:templateIDsWithDefaultWorkflow.Count -eq 0)
Write-Warning "No templates found which use the workflow."
Write-Host "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment