I would like to update a workflow (any Standard Field) for all
- existing items.
- new items.
What is the best way to do this? Can I use Sitecore Rocks or Sitecore Powershell Extensions? Any other better way?
You can use below powershell script (https://gist.github.com/marcduiker/950e0358bb4752ed5b047931a8c958c1) :
# 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
#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 `
-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
GetTemplatesWhichUseTheWorkflow
if ($script:templateIDsWithDefaultWorkflow.Count -eq 0)
{
Write-Warning "No templates found which use the workflow."
}
else
{
ProcessContentItems
}
Write-Host "Done."
To assign workflow to an item please follow bellow link :
https://sdn.sitecore.net/upload/sitecore6/workflowreference-usletter.pdf You need to assing on standard values of the template Default Workflow.
Here is a powershell script to update all templates standard values. Just update the guid and the path to your templates. Note that you can easily enhance this script to adapt for existing items on the content tree as well. This should get you going:
$templates = Get-ChildItem -Path "/sitecore/templates/My Project Templates" -Recurse | Where-Object { $_.Name -match "__Standard Values" } | ForEach-Object{
$_.__Workflow = "{A5BC37E7-ED96-4C1E-8590-A26E64DB55EA}"
}
To update the workflow for all items in a Sitecore instance, you can use a PowerShell script. Here's a general outline of how you can achieve this:
script :
# Define the ID or path of the target workflow
$workflowIdOrPath = "YourTargetWorkflowIDorPath"
# Get the workflow item
$workflowItem = Get-Item -Path "master:" -ID $workflowIdOrPath
# Check if the workflow item exists
if ($workflowItem -eq $null) {
Write-Host "Workflow not found."
return
}
# Get all items in the content tree
$allItems = Get-ChildItem -Path "master:" -Recurse
# Loop through each item
foreach ($item in $allItems) {
# Check if the item has a workflow
if ($item.Fields["__Workflow"] -ne $null) {
# Update the workflow for the item
$item.Editing.BeginEdit()
$item["__Workflow"] = $workflowItem.ID.ToString()
$item.Editing.EndEdit() | Out-Null
Write-Host "Workflow updated for $($item.Paths.Path)"
}
}
Write-Host "Workflow update process completed."