Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patelcp/d4da2a7699fb8c3e9f97d4ee3eba3b00 to your computer and use it in GitHub Desktop.
Save patelcp/d4da2a7699fb8c3e9f97d4ee3eba3b00 to your computer and use it in GitHub Desktop.
Sitecore PowerShell script that creates a report listing content items which has no workflow set while their templates have the Default workflow field set.
<#
.SYNOPSIS
Update Workflow and Workflow state of content items which have no workflow set while their templates have the Default workflow field set.
.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 selected Workflow 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.
The script will retrieve content items which match those templates and have no workflow assigned to them (so it won't overwrite existing workflows/states).
The items will be shown in a report where two actions will be available:
- Update workflow for all items
- Update workflow for selected items
.NOTES
Written by Marc Duiker (https://twitter.com/marcduiker), May 2016
#>
function GetTemplatesWhichUseTheWorkflow {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[Sitecore.Data.ID]$WorkflowID
)
$itemsWithMatchingDefaultWorkflow = Get-Item -Path master: -Query "/sitecore/templates//*[@__Default workflow='$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 GetContentItems()
{
$props = @{
ActionData = @{ WorkflowID = $workflowItem.ID; WorkflowStateID = $workflowStateItem.ID }
Title = $windowTitle
InfoTitle ="List of content items matching the templates that use the workflow: $($workflowItem.Name)."
InfoDescription = "Step 3 - In order to update the workflow and workflow state fields either click the 'Update workflow for all items' action or manually select the items you wish to update and click the 'Update Workflow for selected items' action in the ribbon."
PageSize = 25
ViewName = "ContentItemsWithMissingWorkflow"
}
# Get only the content items for the matching templateIDs and where the Workflow field is empty.
Get-ChildItem -Path master:/content `
-Recurse | `
Where-Object { ($script:templateIDsWithDefaultWorkflow.Contains($_.TemplateID)) -and ($_.__Workflow -eq "") } | `
Show-ListView @props `
-Property @{ Label="Display Name"; Expression={ $_.DisplayName } },
@{ Label="Template"; Expression={ $_.TemplateName } },
@{ Label="Workflow ID"; Expression={ $_.__Workflow } },
@{ Label="Workflow state ID"; Expression={ $_."__Workflow state" } }
}
$windowTitle = "Update Workflow and Workflow state on content items"
# Select the workflow
$workflowItem = Get-Item master:\system\Workflows
$workflowItemResult = Read-Variable -Parameters `
@{ Name = "workflowItem"; Title="Workflow"; Root="/sitecore/system/Workflows" } `
-Description "Step 1 - Select the Workflow which is required for the content items." `
-Title $windowTitle `
-Width 600 `
-Height 200 `
-OkButtonName "Proceed" `
-CancelButtonName "Abort" `
-ShowHints
if($workflowItemResult -ne "ok")
{
Exit
}
# Select the workflow state
$workflowStateItem = $workflowItem
$workflowStateItemResult = Read-Variable -Parameters `
@{ Name = "workflowStateItem"; Title="Workflow state"; Root=$workflowItem.Paths.Path } `
-Description "Step 2 - Select the Workflow state which is required for the content items." `
-Title $windowTitle `
-Width 600 `
-Height 200 `
-OkButtonName "Proceed" `
-CancelButtonName "Abort" `
-ShowHints
if($workflowStateItemResult -ne "ok")
{
Exit
}
# 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
GetTemplatesWhichUseTheWorkflow($workflowItem.ID)
if ($script:templateIDsWithDefaultWorkflow.Count -eq 0)
{
Show-Alert -Title "No templates found which use the workflow. Please make sure the Default workflow field is set on the __Standard Values of the templates that require to use the workflow." -ErrorAction Stop
}
else
{
GetContentItems
}
Close-Window
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment