By admin • January 22, 2024
While working on a Sitecore SXA project, I needed to create a new site by cloning an existing one and performing various updates. As part of this process, I ended up with a large number of items in a draft state, which needed to be transitioned to the final workflow state. Manually publishing each item was not feasible due to the sheer volume, so I created a PowerShell script to automate this task by updating the workflow state of all items to the final state.
In this post, I’ll walk you through the problem, the solution, and provide a cleaned-up version of the PowerShell script I used to accomplish this.
The Problem
In Sitecore, when creating or cloning a site, content items often remain in the draft state or some intermediate workflow state. If you have a lot of items, moving them to the final state manually via the Sitecore interface is tedious and time-consuming.
The solution? Use Sitecore PowerShell Extensions (SPE) to script the process, which allows you to:
- Automate the transition of items to the final workflow state.
- Handle multiple languages and content tree hierarchies.
- Output a summary of the total items checked and updated.
The Solution: PowerShell Script to Update Workflow States
Here’s the cleaned-up version of the PowerShell script I used to change the workflow state for all items under a specific site path to the final state. This script runs in the context of Sitecore’s SPE module.
PowerShell Script for Updating Items to Final Workflow State
# Begin a bulk update context to optimize the process
New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) {
# Define the root path of the site you want to update
$rootItemPath = "master:/sitecore/content/YourSite/YourSiteName"
# Define the languages you want to support
$languages = @("en") # Add more languages as needed
# The final workflow state GUID (replace with your own)
$workflowFinalState = "{Your-Final-Workfow-State-ID}"
$updatedItemsCount = 0
$checkedItemsCount = 0
# Hashtable to track updated items by language
$updatedItemsPerLanguage = @{}
# Loop through each language
foreach ($language in $languages) {
# Initialize count for this language
$updatedItemsPerLanguage[$language] = 0
# Get the root item in the current language
$rootItem = Get-Item -Path $rootItemPath -Language $language
# Retrieve the root item and its children
$items = @($rootItem) + (Get-ChildItem -Item $rootItem -Recurse -Language $language)
foreach ($item in $items) {
if ($item -ne $null) {
$checkedItemsCount++
# Get the current workflow state
$currentWorkflowState = $item["__Workflow state"]
# If the item's workflow state is not already in the final state, update it
if ($currentWorkflowState -ne $workflowFinalState) {
$item.Editing.BeginEdit()
$item.Fields["__Workflow state"].Value = $workflowFinalState
$item.Editing.EndEdit() > $null
Write-Host "$($item.Paths.FullPath) in $language --> Updated"
$updatedItemsCount++
$updatedItemsPerLanguage[$language]++
} else {
Write-Host "$($item.Paths.FullPath) in $language --> Already in the desired state"
}
}
}
}
# Output summary of the process
Write-Host "Total items checked: $checkedItemsCount"
Write-Host "Total items updated: $updatedItemsCount"
# Display the number of updated items per language
foreach ($lang in $languages) {
Write-Host "Items updated in $lang: $($updatedItemsPerLanguage[$lang])"
}
}
How the Script Works:
- Bulk Update Context: The script begins with
New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext)
to ensure the updates are processed efficiently and without performance bottlenecks. This is crucial when updating many items. - Language Support: The
$languages
array allows you to define the languages in which the items exist. In this example, it's set to"en"
for English, but you can add other language codes like"fr"
,"de"
, etc. - Final Workflow State: The
$workflowFinalState
variable holds the GUID of the final workflow state. You’ll need to replace this with the correct GUID for the final state in your workflow. - Recursive Item Fetching: The script retrieves the root item and its children using
Get-ChildItem
with the-Recurse
flag. This ensures all items under the root path are included. - Workflow State Check: For each item, the script checks if it’s already in the final workflow state. If not, it updates the
__Workflow state
field and commits the change. - Summary: After processing, the script outputs a summary showing how many items were checked and updated, broken down by language.
How to Run the Script
- Install Sitecore PowerShell Extensions (SPE): If you don’t already have SPE installed on your Sitecore instance, you’ll need to install it. This module allows you to execute PowerShell scripts inside Sitecore.
- Run the Script: Open the PowerShell ISE or PowerShell Console in Sitecore under the Control Panel and paste the script. Adjust the
$rootItemPath
,$languages
, and$workflowFinalState
as necessary for your environment. - Execute: Run the script. The output will show the paths of the items being updated and provide a summary at the end.
When Should You Use This?
This script is handy when:
- You’ve cloned a site and need to batch update all items to their final workflow state.
- Items remain stuck in draft or intermediate workflow states after deployment.
- You’re working with multilingual sites and need to ensure content across different languages is published.
- You want to automate workflow transitions for large sets of content.
Conclusion
Automating workflow state transitions in Sitecore using PowerShell not only saves time but also ensures consistency across large batches of content items. By using this script, you can efficiently update thousands of items, taking care of language-specific versions and ensuring everything is in the correct final workflow state.