1

I'm building a Sitecore powershell script where I'm using Read-Variable to read a few fields. I have added the Mandatory=$true on all of them and it only works for single-line fields but not for droptree ones. Anyone know how I can make it mandatory?

Here is my script:

$item = Get-Item master:\content\home 
$result = Read-Variable -Parameters `    @{ Name = "token"; Value="Token"; Title="Value to be replaced"; Tooltip="Value to be replaced"; Placeholder="Inform what text you want to replace"; Mandatory=$true},
@{ Name = "replaceWith"; Value="Replace With"; Title="New value"; Tooltip="New Value"; Placeholder="Inform the new value"; Mandatory=$true}, 
@{ Name = "item"; Title="Select the root item. It will apply to all it's descendants"; Root="/sitecore/content/"; Mandatory=$true} `
-Description "This Dialog shows less editors, it doesn't need tabs as there is less of the edited variables" `
-Title "Initialise various variable types (without tabs)" -Width 500 -Height 480 -OkButtonName "Proceed" -CancelButtonName "Abort"
4

TL;DR

You are not using DropTree but DropLink. I don't think you can set null value for DropLink type. It has always some value (clearing it on client side input won't change anything)

Read more because there is a solution for that

Why Mandatory is not working

Mandatory field should work fine anyway. The reason your value for $item variable is never null/empty is fact that you are assigning default value at the beginning

$item = Get-Item master:\content\home 

Change it to

$item = $null

in your initial implementation and you will see that validation with Mandatory parameter work, though this field will not be DropLink anymore, it is SingleLineText now (I am not sure, maybe we should report this to SPE team?). enter image description here

How to solve the problem

Default field used in you case is DropLink. We have to declare that we want to use DropTree explicitly.

Steps:

  • Add Additional parameter editor="DropTree" to your item variable
  • Switch Root parameter name to Source

From now on you can set empty value for your item selector.

Complete example:

In this example you can see how to use Mandatory parameter and -Validator switch at the same time.

$item = Get-Item "master:\content\home"
$item = $null
$result = Read-Variable -Parameters `
    @{ Name = "token"; Value = "Token"; Title = "Value to be replaced"; Tooltip = "Value to be replaced"; Placeholder = "Inform what text you want to replace"; Mandatory = $true },
    @{ Name = "replaceWith"; Value = "Replace With"; Title = "New value"; Tooltip = "New Value"; Placeholder = "Inform the new value"; Mandatory = $true },`
    @{ Name = "item"; editor="DropTree";Title = "Select the root item. It will apply to all it's descendants"; Root = "/sitecore/content/"; Mandatory = $true  } `
    -Description "This Dialog shows less editors, it doesn't need tabs as there is less of the edited variables" `
    -Title "Initialise various variable types (without tabs)" -Width 500 -Height 480 `
    -OkButtonName "Proceed" -CancelButtonName "Abort" `
    -Validator {
        $itemVariable = $variables.item.Value
        if ($itemVariable.ID -eq "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}")
        {
            $variables.item.Error = "Please select different root item"
        }
}

enter image description here

Summary

Validator feature is available for SPE 4.2+

Links

  • 1
    hi Alan I'm probably missing something silly here. I have updated the item section to be like this: @{ Name = "item"; Title="Select the root item. It will apply to all it's descendants"; Root="/sitecore/content/WAL"; Validator= { if($rootItem -eq $null) { $variables.item.Error = "Please select root item" } } } ` $Variable:item If I leave it blank it will show the last one selected but will not show the error message. Any ideas on what I'm doing wrong? – Diego Dec 8 '16 at 14:16
  • 1
    @Diego my answer has been updated. – Alan Płócieniak Dec 9 '16 at 7:04
  • I tested and it works with this new example. thanks Alan! – Diego Dec 9 '16 at 12:16

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.