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.
__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:42New-UsingBlock (New-Object Sitecore.SecurityModel.SecurityDisabler) { $version.Editing.BeginEdit(); $version."__Workflow state" = $workflowApprovedStateItemId $version.Editing.EndEdit(); }
– Hishaam Namooya Jul 24 '18 at 7:41cmdlet 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