The other day I had a requirement where I had to implement a new workflow on a Sitecore implementation. All of the existing templates needed to begin using this new workflow. As I’m a developer, every time I need to perform an action that is repetitive, I think about ways to automate it. In this case, since there were a few templates that needed to be updated, I thought about creating a simple Sitecore PowerShell script to update all of them.
Sitecore PowerShell Extensions is a great module to automate things that would otherwise require repetitive, time consuming, manual steps. Also, if you are a Sitecore developer and you are not using it yet, I would highly recommend you give it a try. It comes with a lot of nice features like reporting, integrations with Content Editor and Control Panel, as well as a ton of commands you can use to build your scripts and manipulate Sitecore data easily.
The Script
Ok, so back to my requirement. I came up with the following script (slightly adapted for this post):
$templates = Get-ChildItem -Path "/sitecore/templates/My Project Templates" -Recurse | Where-Object { $_.Name -match "__Standard Values" } | ForEach-Object{ $_.__Workflow = "{A5BC37E7-ED96-4C1E-8590-A26E64DB55EA}" }
What this script is doing is getting all the standard values from the templates that are descendants from the path “/sitecore/templates/My Project Templates”, which is where the templates for my project were created. Then it is looping all the results and setting on the workflow field the GUID of the workflow I created which in this case was “{A5BC37E7-ED96-4C1E-8590-A26E64DB55EA}”.
Getting Started with PowerShell
Simple, right? Well that’s why I personally like to use the Sitecore PowerShell Extensions module as much as it makes sense, because it can make life easier. If you want, you can download it directly from here. Other resources to help you get going include this online book and community support on the #module-spe Slack channel.
Happy Sitecoring 🙂
Very nice! I recently had to implement something very similar. Here’s what I came up with (assuming $workflow and $finalState are guids, while $contentItems is a collection of pages which should have workflow):
===
# apply workflow to standard values
Get-ChildItem -Recurse “master:/path/to/page/templates” | Where-Object { $_.Name -match “__Standard Values” } | ForEach-Object {
$_.”__Default workflow” = $workflow
}
# apply workflow to content items
$contentItems | ForEach-Object {
$_.__Workflow = $workflow
$_.”__Workflow state” = $finalState
}
===
Always love seeing people use this module.