0

How to write sitecore powershell script to change WorkFlow status to Approved from particular Media Library Folder.

I have multiple documents to replace in bulk in Sitecore Media Library and have workflow enabled. And I want to change WorkFlow Status to Approved by Sitecore Powershell from particular Media Library Folder. Is there any way to do that?

Thank you

2 Answers 2

1

You can use below script as well. It uses BulkUpdateContext to make the operation faster and it looks through all the versions of media items for multilingual scenarios. Rest of the basics of item editing are same as in script shared by Sumit -

$rootItem = Get-Item -Path master:"Media Folder PATH"
$workflowApprovedStateID = "WORKFLOW APPROVED STATE GUID"
$items = Get-ChildItem $rootItem.ID -Recurse -Language * -Version *
New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) {
    $items | ForEach-Object {
        $item = $_
        $item.Editing.BeginEdit();
        $item.Fields["__Workflow state"].Value = $workflowApprovedStateID 
        $item.Editing.EndEdit();
        Write-Host "$($item.FullPath)  --> updated to approved workflow state"
    }
}
4
  • 3
    While this will change the field value, it will not run any actions associated with moving the item to the final workflow state.
    – Richard Seal
    Commented Sep 14, 2022 at 13:32
  • 3
    Its not the BulkUpdateContext that is the problem. Changing the field value manually does not trigger any workflow actions. You need to get the WorkflowProvider for the workflow and item and execute the next step.
    – Richard Seal
    Commented Sep 14, 2022 at 13:43
  • Thanks Richard! I will test updating workflow state using WorkflowProvider in powershell and will try to add that option in the answer. Apologies, I ended up deleting my previous comment before seeing your response after that.
    – Ghan
    Commented Sep 14, 2022 at 13:55
  • 1
    Thank you for quick support. The above script worked fine. Commented Sep 16, 2022 at 5:42
0

You can wite you code something like this.

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

Hope this helps.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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