PowerShell Scripts

This page contains a library of useful PowerShell scripts that aren’t found on other sites or blogs –
 
 

Workflow History

The script compiles a report of workflow-related events in a single view for all item versions.

$item = $SitecoreContextItem
$db = $SitecoreContextItem.Database
$workflowProvider = $db.WorkflowProvider

$events =@()
$scEvents=@()
$versions = $item.Versions.GetVersions() | Sort-Object Version -Descending
foreach($version in $versions) {
    $scEvents = $workflowProvider.HistoryStore.GetHistory($version) 
    foreach($scEvent in $scEvents){
        $event = @{ Icon = $scEvent.__Icon
            User = $scEvent.User
            Date = $scEvent.Date
            OldState = if([string]::IsNullOrEmpty($scEvent.OldState)) {"-"}else{ (Get-Item -Path $scEvent.OldState).DisplayName }
            NewState = if([string]::IsNullOrEmpty($scEvent.NewState)) {"-"}else{ (Get-Item -Path $scEvent.NewState).DisplayName }
            Version = $version.Version.Number
            Comment = $scEvent.CommentFields["Comments"] 
        }
        $events +=$event
    }
}

$props = @{
        InfoTitle = "Workflow History"
        InfoDescription = "Displays workflow history for a selected item."
        PageSize = 25
    }


$events | Show-ListView @props -Property @{Label="Date"; Expression={$_.Date} },
            @{Label="Icon"; Expression={$_.Icon} },
            @{Label="User"; Expression={$_.User} },
            @{Label="Old State"; Expression={ $_.OldState } },
            @{Label="New State"; Expression={ $_.NewState } },
            @{Label="Version"; Expression={$_.Version} },
            @{Label="Comment"; Expression={$_.Comment} }
 

 

Recursive Locking and Unlocking

The following scripts can be helpful for large content teams working on dedicated areas of content. Using the SPE these scripts can be either placed in the context menu, or in the ribbon of the Content Editor. The item locking and unlocking features prompt users if they would like to have the action applied to the item’s children, if they exist.

 

Locking

The locking script is sophisticated enough to respect existing locks by other users and only locks items that do not already have a lock on them. At the same time the locking feature also respects versions already participating in a content promotion workflow, thus, if the latest version is in a state other than the initial one, a new version is created and locked to the user.

if($SitecoreContextItem -ne $null){
        $selectedItems = @();
        $selectedItems += $SitecoreContextItem
        if($SitecoreContextItem.Children.Length -gt 0){
            $includeChildrenPromptResult = Show-ModalDialog -Control "ConfirmChoice" -Parameters @{btn_0="Yes"; btn_1="No";  te="Lock child items?"; cp="Lock Item"} -Height 120 -Width 450
            if($includeChildrenPromptResult -eq "btn_0"){
                $allUnlockedChildItems = Get-ChildItem -Path $SitecoreContextItem.Paths.FullPath -Recurse | where { -not $_.Locking.IsLocked()}
                $selectedItems += $allUnlockedChildItems
            }
        }

        foreach($item in $selectedItems){
            $currentStateName = if(!$item.Fields["__Workflow state"].Value) {""}else{ (Get-Item -Path $item.Fields["__Workflow state"].Value).DisplayName } 
            if($currentStateName -and $currentStateName -ne "Draft"){
                $newVersion = Add-ItemVersion -Id $item.ID -IgnoredFields "__Workflow state"
                $item = $newVersion
            }
        
            $result = $item.Locking.Lock()
            if($result -eq $false){
                Write-Host "Unable to lock " $item.Paths.FullPath". Likely caused by an existing lock or lack of write access."
            }
                
        }
}

 

Unlocking

The unlocking feature only unlocks items that are locked to the acting user.

 

if($SitecoreContextItem -ne $null){
        $selectedItems = @();
        if($SitecoreContextItem.Locking.IsLocked()){
            $selectedItems += $SitecoreContextItem    
        }
        
        if($SitecoreContextItem.Children.Length -gt 0){
            $includeChildrenPromptResult = Show-ModalDialog -Control "ConfirmChoice" -Parameters @{btn_0="Yes"; btn_1="No";  te="Unlock child items?"; cp="Unlock Item"} -Height 120 -Width 450
            if($includeChildrenPromptResult -eq "btn_0"){
                $allLockedChildItems = Get-ChildItem -Path $SitecoreContextItem.Paths.FullPath -Recurse | where { $_.Locking.IsLocked()}
                $selectedItems += $allLockedChildItems
            }
        }
        
        foreach($item in $selectedItems){
                $itemLockingProvider = New-Object -TypeName Sitecore.Data.Locking.ItemLocking -ArgumentList $item
                $owner = $itemLockingProvider.GetOwner()
                $user = (Get-User -Current).Name

                if($owner -ne $user){ 
                    Write-Host "Unable to unlock " $item.Paths.FullPath". $owner already has a lock on the item."
                    continue
                }
                $result = $item.Locking.Unlock()
                if($result -eq $false){
                    Write-Host "Unable to unlock " $item.Paths.FullPath". Likely caused by lack of write access."
                }
        }
}

Summary
Sitecore PowerShell Scripts
Article Name
Sitecore PowerShell Scripts
Author
Publisher Name
Vasiliy Fomichev
Publisher Logo