3

I have a script that loops through items with a certain name, then loops through the versions of that item and checks the workflow standard fields (__Workflow, __Workflow state, etc). The goal of the script is to clear out a bunch of old items from the workbox that cannot be removed manually (separate issue), by setting the state field to the approved state.

This is working well for some of the versions, but for others, if I add a watch in the immediate window for $version.__Workflow (or $version."__Workflow state") I get nothing back, despite definitely seeing a value on that item version in the content editor. It's really odd. I did notice that, for the versions where the __Workflow field is empty, it is only happening for version 1 where there are at least 2 versions and the 2nd or 3rd version is approved. For the others that didn't have this problem, I checked a few and noted that there was only one item version.

My code:

# Set location for script to run
Write-Host "Starting work in the context of the 'master' database, from the 'Center List Page' item."
Set-Location -Path "master:\content\Centers New\Center List Page"

# Set up variables
$workflowItemId = "{C911EB2D-F425-40F1-9BD0-E0BEF9061828}"
$workflowApprovedStateItemId = "{D520C586-7F39-4591-9D0A-730CACEDD706}"
$itemsProcessed = 0;
$itemVersionsProcessed = 0;
$itemVersionsUpdated = 0;

# Logic
foreach ($item in Get-ChildItem . -Recurse)
{
    $itemsProcessed = $itemsProcessed + 1;

    if ($item.Name -eq "Local Image")
    {
        foreach ($version in $item.Versions.GetVersions())
        {
            # {A4505DF8-2C8D-42E0-AD39-45400B1A78E4} is an example of one still in draft.
            $itemVersionsProcessed = $itemVersionsProcessed + 1;

            if ($item.ID -eq "{A4505DF8-2C8D-42E0-AD39-45400B1A78E4}") {
                $test = "stop"
            }

            if ($version.__Workflow -eq $workflowItemId -And $version."__Workflow state" -ne $workflowApprovedStateItemId)
            {
                 Write-Host "-- Updating item workflow status to 'Approved'... --"
                 $version.Editing.BeginEdit();
                 $version."__Workflow state" = $workflowApprovedStateItemId
                 $version.Editing.EndEdit();
                 $itemVersionsUpdated = $itemVersionsUpdated + 1;
            }
        }
    }
}
Write-Host "The script has completed."
Write-Host ("{0} items have been processed." -f $itemsProcessed)
Write-Host ("{0} item versions have been processed." -f $itemVersionsProcessed)
Write-Host ("{0} item versions had their workflow state field updated to Approved." -f $itemVersionsUpdated)

EDIT: If I try to set the __Workflow field through the immediate window, I get this error: The property '__Workflow' cannot be found on this object. Verify that the property exists and can be set.

enter image description here

  • Does the field Workflow populated with the workflow item? If not it won’t go through to update the workflow because the condition will not be met. I mean the IF statement before performing the edit – Hishaam Namooya Jul 23 '18 at 20:53
  • @HishaamNamooya Yes, version 1 of that item does have the ID of the workflow in the __Workflow field. Thus my confusion. I even turned on raw values and saw the ID there in the field. – Paul Jul 23 '18 at 22:42
  • Can you also use the SecurityDisabler block: New-UsingBlock (New-Object Sitecore.SecurityModel.SecurityDisabler) { $version.Editing.BeginEdit(); $version."__Workflow state" = $workflowApprovedStateItemId $version.Editing.EndEdit(); } – Hishaam Namooya Jul 24 '18 at 7:41
  • @HishaamNamooya Hmm.. the problem is the if statement before that. I tried adding your block around the if statement but then when I debug it opens a dialog saying: cmdlet New-UsingBlock at command pipeline position 1. Supply values for the following paramters: ScriptBlock. If I enter some garbage text it keeps appearing. – Paul Jul 24 '18 at 17:11
0

Using $version.Fields["__Workflow"].Value instead of $version.__Workflow (and similarly for the __Workflow state field) did the trick. Not sure why some of the item versions did not have a __Workflow property.

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.