Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vtml/98c1a0540065c7c2f7f1309bdca9e7ac to your computer and use it in GitHub Desktop.
Save vtml/98c1a0540065c7c2f7f1309bdca9e7ac to your computer and use it in GitHub Desktop.
Sitecore Powershell script to update the workflow and state on content items which do not have a workflow set on them.
<#
.SYNOPSIS
Updates the Workflow and Workflow state fields of content items with the given $workflowID and $workflowStateID values.
.DESCRIPTION
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).
.NOTES
Only the $workflowID and $workflowStateID variables need to be set by the user.
.AUTHOR
Written by Marc Duiker on May 6th 2016.
#>
#region workflow specific variables
# Corporate WorkFlow
# WorkFlowID: {92E7DED1-1A4E-46B1-BB12-C81B97739680}
# Final State: {82C884B5-0D6B-46A2-A24F-E11938A21CC3}
# Datasource Workflow
# WorkFlowID: {35E42E3C-DC4D-42CD-8F16-D5EEBA67A49C}
# Final State: {64C7D06C-D870-401E-BC64-17063B873362}
# This is the ID of the workflow that will be set on the content items.
$script:workflowID = "{35E42E3C-DC4D-42CD-8F16-D5EEBA67A49C}" #Sample Workflow
# This is the ID of the workflow state that will be set on the content items.
$script:workflowStateID = "{64C7D06C-D870-401E-BC64-17063B873362}" #Approved State
#endregion
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
$script:itemCount++
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\CPA\Corporate `
-Recurse | `
Where-Object { ($script:templateIDsWithDefaultWorkflow.Contains($_.TemplateID)) -and ($_."__Workflow state" -ne $script:workflowStateID) } | `
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
GetTemplatesWhichUseTheWorkflow
if ($script:templateIDsWithDefaultWorkflow.Count -eq 0)
{
Write-Warning "No templates found which use the workflow."
}
else
{
Suspend-SearchIndex -Name sitecore_master_index
Suspend-SearchIndex -Name sitecore_sxa_master_index
ProcessContentItems
Resume-SearchIndex -Name sitecore_master_index
Resume-SearchIndex -Name sitecore_sxa_master_index
}
Write-Host "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment