Using sitecore 10.0.1 Currently, I want to move items under a path, to "Approve" workflow. So how to fetch items that are not in approved state and change them to "Approved" workflow using powershell script
2 Answers
You can use below code, in which {0729C93B-888A-4765-8486-8F1AE86A3894}
is orkflow state id.
$itemsWithMatchingDefaultWorkflow = Get-Item -Path master: -Query "/sitecore/content/*[@__Workflow='{0729C93B-888A-4765-8486-8F1AE86A3894}']"
foreach ($item in $itemsWithMatchingDefaultWorkflow)
{
Write-Host " -" $item.ID $item.Paths.FullPath
}
Or you can use this as well -
Get-ChildItem -Path master:\sitecore\content -Recurse | Where-Object { $_.__Workflow -eq "{0729C93B-888A-4765-8486-8F1AE86A3894}"}
And to update you can use below -
$rootItem = Get-Item -Path master:"START PATH"
$language = "en"
$workflowFinalState = "WORKFLOW STATE GUID"
foreach ( $item in Get-ChildItem -Item $rootItem -Recurse -Language $language)
{
$item.Editing.BeginEdit();
$item.Fields["__Workflow state"].Value = $workflowFinalState
$item.Editing.EndEdit();
Write-Host "$($item.FullPath) --> Updated"
}
Or you can use this as well to update workflow -
$itemsWithMatchingDefaultWorkflow = Get-ChildItem -Path master:\sitecore\content -Recurse | Where-Object { $_.__Workflow -eq "Workflow state id"}
foreach ($item in $itemsWithMatchingDefaultWorkflow)
{
$item.__Workflow = "Workflow final state id"
}
-
-
-
-
Try this - Get-ChildItem -Path master:\sitecore\content -Recurse | Where-Object { $_.__Workflow -eq "{0729C93B-888A-4765-8486-8F1AE86A3894}"} Oct 18, 2022 at 7:31
-
1
Try using this approach.
# ID of the workflow that will be set on the content items. Ex. Sample Workflow
$script:workflowID = "{A5BC37E7-ED96-4C1E-8590-A26E64DB55EA}"
# ID of the workflow state that will be set on the content items. Ex. Approved State
$script:workflowStateID = "{FCA998C5-0CC3-4F91-94D8-0A4E6CAECE88}"
# Create a loop recursively under a tree node like this.
$processedItems = Get-ChildItem -Path master:/content/Home -Recurse | ForEach-Object {
$_.__Workflow = $script:workflowID
$_."__Workflow state" = $script:workflowStateID
}
It will change the state to Approve.