10

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?

9

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.

  • But how will i force for the all new items created ? – Prakash Jun 15 '17 at 13:41
2

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}"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.