GUI application script

Recently, I have had to create a PowerShell script to modify selected items. Consequently, I have created GUI application script in Sitecore PowerShell Extension. If you have ever had the same problem this is the right place to be :P.

Preconditions:

Let's go straight to code

Set-Location master:

Write-host "Start script"

$source = "query:/sitecore/content/Home//*"

$result = Read-Variable -Parameters `
@{
     Title="Multilist title"; 
     Editor="multilist"
     Source= $source;
     Name = "selectedItems";
} `
-Description "Window description" `
-Title "Window title" -Width 450 -Height 300 -OkButtonName "Confirm" -CancelButtonName "Cancel" -ShowHints

if($result -eq "ok") {
    Write-Host "Print choosen elements"
    foreach($item in $selectedItems) {
        Write-Host $item.ID
        $versions = Get-Item master -ID $item.ID -Language *
        
        foreach($version in $versions){
            Write-Host "$($version.Paths.Path) [$($version.Language)]"
        }
    }
}
else {
    Write-Host "Canceled"
}

$selectedItems = ""

Write-Host "Finished"

The first line sets the Sitecore database to master, then the "$source" variable specifies the location of items which are gonna be displayed on the list.

The next section defines the window parameters like window title, description and editor parameters like title, editor type(multi-list in this case), source location and name of the variable which contains the selected items. The "$result" variable contains bool value which determines whether the of not you clicked the "Confirm" button.

The "foreach" loop iterates through the selected items, gets all version and prints path and language per each version.

The last line that might look interesting is $selectedItems = "". I assign an empty string to this variable because otherwise when you run the script again you the list will be filled with previously selected items.

Showtime

When you run the above script you get:

GUI application script

Multilist

You get the following result which contains all versions paths and languages of selected items:

script-output

Script output

Additional examples

You can also try editor types like password, tree-list or if you just set Editor parameter to empty then you get a single-line text field.

Password field

Password field

Treelist field

Treelist field

I hope it was helpful and see you next time. 🙂

 

Subscribe to my newsletter:

Categories: powershell

Leave a Reply

Your email address will not be published. Required fields are marked *