Forum Discussion

Blair24's avatar
Blair24
Copper Contributor
Nov 29, 2018
Solved

PnP Site Creation - Site Choice Column Values switch

Hi Folks, I have a PnP Template which has a site choice column called "Options" that's applied to 5 document libraries in the template. When the site creation script is running an array of choices...
  • Matt Weston's avatar
    Matt Weston
    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.

Resources