Forum Discussion
PnP Site Creation - Site Choice Column Values switch
- Dec 04, 2018
Hi Blair24 sorry for the delay getting back to you. You could try something similar to this:
$siteurl = ""
Connect-PnPOnline -Url $siteurl -UseWebLogin
$context = Get-PnPContext
# Remove one of the following dependent on what you want to test
# Single Choice field which I tested with the choice "Choice 1"
#$choicefield = Get-PnPField -Identity "OneChoice"
#$choicecount = $choicefield.Choices.Count
# Multiple Choice field which I tested with the choices "Choice 1, Choice 2, Choice 3"
$choicefield = Get-PnPField -Identity "MultipleChoice"
$choicecount = $choicefield.Choices.Count
# Switch logic based on the number of choices assigned to the field
# If it has more than 1, then set the Default to "blank"
switch($choicecount)
{
1 { $choicefield.DefaultValue = $choicefield.Choices[0] }
default { $choicefield.DefaultValue = $null }
}
# Update the field and propogate changes to anything using the column
$choicefield.UpdateAndPushChanges($true)
$context.ExecuteQuery()Whilst it may not be 100% what you need, hopefully the example will help you to build that into your solution.
It's one site template with one site choice column, felt it was easier having one column that has a default value when there's only one "choice" and then has no default value when there's more than one choice in the column.
The choices (regardless of the number) will be passed in to a azure function app as a single variable which will run the PS.
Hi Blair24 sorry for the delay getting back to you. You could try something similar to this:
$siteurl = ""
Connect-PnPOnline -Url $siteurl -UseWebLogin
$context = Get-PnPContext
# Remove one of the following dependent on what you want to test
# Single Choice field which I tested with the choice "Choice 1"
#$choicefield = Get-PnPField -Identity "OneChoice"
#$choicecount = $choicefield.Choices.Count
# Multiple Choice field which I tested with the choices "Choice 1, Choice 2, Choice 3"
$choicefield = Get-PnPField -Identity "MultipleChoice"
$choicecount = $choicefield.Choices.Count
# Switch logic based on the number of choices assigned to the field
# If it has more than 1, then set the Default to "blank"
switch($choicecount)
{
1 { $choicefield.DefaultValue = $choicefield.Choices[0] }
default { $choicefield.DefaultValue = $null }
}
# Update the field and propogate changes to anything using the column
$choicefield.UpdateAndPushChanges($true)
$context.ExecuteQuery()
- Blair24Dec 07, 2018Copper Contributor
What worked cheers!
Just a quick second question sorry!
Is it possible to add choices to a choice column via PowerShell, trying to run the following PS:$CustomerNames.Choices.Add($namechoices)
$CustomerNames.Update()But keep getting the following error:
Exception calling "Remove" with "1" argument(s): "Collection was of a fixed size."
- Matt WestonDec 07, 2018Iron Contributor
You're 99% there, just treat the choices as a string rather than an array.
$fieldChoice.Choices += "New Choice"$fieldChoice.Update()- Blair24Dec 08, 2018Copper Contributor
That's brilliant cheers!
So if you wanted to remove a choice, would it be the same principle - treat is as a string?
As ChoiceField.Choices.Remove(0) gives you an array error.