0

I have a hash table with keys as Guids and values as string. Of which some of the values are same with different Guids. User will be presented with an prompt which contains a droplist of options and the selected option from the droplist will be selected as an input for next step. In order to achieve this, I'm trying to display values in the drop down list but I'm getting Guids/keys instead. Is there a way to display values which are strings(and have duplicates) instead of Guids?

$options = [Ordered]@{}
Foreach ($i in $Items) // $Items contains list of items
{
 $options +=@{i.Id = $i.DisplayName}
}

$options.keys is displaying all the guids $options.values is displaying the names. However when I try to display them to allow users to select an input, I'm getting Guids in the drop-down list

$props = @{
    Parameters = @(
    @{
       Name = "Name"; Title = "Name"; Options = $options
     }
    )
}

Displaying in the dialog Using: $name =Read-Variable @props

1 Answer 1

3

Is there a way to display values which are strings

Yes. Solution later.

and have duplicates

Not really because they are keys and have to be unique.

Solution

Let's consider following case:

enter image description here

Dialog that you can build:

enter image description here

Code

$dialogOptions = New-Object System.Collections.Specialized.OrderedDictionary

# fill values
gci -path '/sitecore/content/Home' | % {
    $dialogOptions.Add($_.Name, $_.ID)
}

# build dialog
$parameters = @()
$parameters += @{ Name = "selectedValues"; Options = $dialogOptions; Editor = "checklist"; Title = 'Selected items' }
Read-Variable -Parameters $parameters
$selectedValues

You will have to handle a case with duplicates manually. You can add index numbers into bracelets or something like that.( Sample Item 1 (1), Sample Item 1 (2))

Use this code to check if key already exists in the dictionary:

$dialogOptions.Contains($name)

1
  • Thanks Alan. Yeah, Manually handling duplicates is the solution. Wish Example1 had option to display value instead of Name. doc.sitecorepowershell.com/appendix/common/read-variable. That way I could have stored Guid as a key and displayed value in the drop down list
    – Dev
    Nov 28, 2021 at 18:40

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.