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.
Hi Blair24 I don't think that's possible within the PnP Template itself, but there's no reason that you couldn't write a few lines of script to run after the ApplyPnPTemplate call to perform that logic. If you need help with that, please let me know.
Yeah was wondering what the best script would be to use after the pnp template has ran. The options would basically come in two variables. The first would be one choice and the second variable could contain multiple choices. So just wondering how I could say if the second variable is blank set the first to the default value?
- Matt WestonNov 30, 2018Iron Contributor
Where is it you're going to be storing the potential values for the choice fields? Do you have multiple templates, one with a single choice, and one with multiple?
Or are you planning on storing the values somewhere else?
- Blair24Dec 01, 2018Copper Contributor
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.- Matt WestonDec 04, 2018Iron Contributor
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.