SharePoint Online: Add Choice Field to List using PowerShell

Requirement: PowerShell to add a choice field to list in SharePoint Online.

How to Add Choice Field to SharePoint Online List?

The choice column in SharePoint Online is used to provide a pre-defined set of choices for the metadata column. Based on the settings you configure, This field may accept a single value or multiple values. When configured for single selection, it is a drop-down list or radio buttons and displayed as checkboxes when we configure it for multiple selections. To add a choice field from Web UI, follow these steps:

  1. Browse to your SharePoint Online site and navigate to the target list in which you want to add the Choice column.
  2. Under the List tab, click on the “Create Column” button in the ribbon.
  3. Provide the Name to your new column, specify the type as “Choice (menu to choose from)” 
    sharepoint online add choice column to list using powershell
  4. Scroll down and enter choices to the given list as one choice per line
    powershell to create choice column in sharepoint list
  5. Fill in other optional values such as Choice field type (Dropdown/Radio button/Checkbox) and click on “OK” to create a Choice field in the SharePoint Online list.

PowerShell to Create Choice Field in SharePoint Online List:

Here is the PowerShell CSOM script to add a choice column to the SharePoint Online list.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Custom function to add column to list
Function Add-ChoiceColumnToList()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $Name,
        [Parameter(Mandatory=$true)] [string] $DisplayName,
        [Parameter(Mandatory=$true)] [string] $ChoiceValues,
        [Parameter(Mandatory=$true)] [string] $DefaultValue,
        [Parameter(Mandatory=$false)] [string] $Description=[string]::Empty,
        [Parameter(Mandatory=$false)] [string] $IsRequired = "FALSE",
        [Parameter(Mandatory=$false)] [string] $EnforceUniqueValues= "FALSE",
        [Parameter(Mandatory=$false)] [string] $Format ="Dropdown",
        [Parameter(Mandatory=$false)] [string] $FillInChoice="FALSE"
    )

    #Generate new GUID for Field ID
    $FieldID = New-Guid

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()

        #Check if the column exists in list already
        $Fields = $List.Fields
        $Ctx.Load($Fields)
        $Ctx.executeQuery()
        $NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
        if($NewField -ne $NULL)  
        {
            Write-host "Column $Name already exists in the List!" -f Yellow
        }
        else
        {
           #Frame Choices
            $ChoiceOptions=[string]::Empty
            $Choices = $ChoiceValues.Split(",")
            foreach ($Choice in $Choices)
            {
                $ChoiceOptions = $ChoiceOptions + "<CHOICE>$Choice</CHOICE>"
            }
            #Define XML for Field Schema
            $FieldSchema = "<Field Type='Choice' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description' Required='$IsRequired' FillInChoice='$FillInChoice' Format='$Format'><Default>$DefaultValue</Default> <CHOICES>$ChoiceOptions</CHOICES></Field>"
            $NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
            $Ctx.ExecuteQuery()    

            Write-host "New Column Added to the List Successfully!" -ForegroundColor Green  
        }
    }
    Catch {
        write-host -f Red "Error Adding Column to List!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$Name="ProjectDepartment"
$DisplayName="Project Department"
$Description="Enter the Project Department"
$ChoiceValues="IT,Sales,Operations,Marketing,HR"
$DefaultValue="IT"
$Format="RadioButtons" #Override default parameter value Dropdown

#Call the function to add column to list
Add-ChoiceColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description -Format $Format -ChoiceValues $ChoiceValues -DefaultValue $DefaultValue

This PowerShell script adds a new choice column for given parameters.

Tips: How to Add MultiChoice Column? Just change “Field Type=’MultiChoice'” in the Field schema XML

Add Choice Field to SharePoint Online List using PnP PowerShell

The Choice column allows users to select from a predefined list of values when entering data into the list. Let’s see how to add a choice field to a SharePoint Online list using PnP PowerShell:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName= "Projects"

#Get Credentials to connect
$Cred = Get-Credential

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred

#Add Choice Field
Add-PnPField -List $ListName -DisplayName "Department" -InternalName "Department" -Type Choice -AddToDefaultView -Choices "Sales","Marketing","Purchase", "IT", "HR" 

Similarly, you can also add a choice column to the SharePoint Online list from XML.

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName= "Projects"

#Get Credentials to connect
$Cred = Get-Credential

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred

#Genrate New GUID for List ID
$FieldID = ([GUID]::NewGuid()).GUID

#Define XML Schema for Choice Field
$FieldXML= "<Field Type='Choice' Name='Priority' ID='$FieldID' DisplayName='Priority'>
                 <CHOICES>
                      <CHOICE>(1) High</CHOICE>
                      <CHOICE>(2) Normal</CHOICE>
                      <CHOICE>(3) Low</CHOICE>
                </CHOICES>
                <Default>(2) Normal</Default>
            </Field>"

#Add Choice Field to list from XML
Add-PnPFieldFromXml -FieldXml $FieldXML -List $ListName

Change the Field type to “MultiChoice” if you need a multiple-choice column. Set Format=’RadioButtons’ for radio buttons choice.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

5 thoughts on “SharePoint Online: Add Choice Field to List using PowerShell

  • Hi!

    Very useful article!

    Is there a way to add a choice value via PnP in a specific position?

    Thanks!!

    Reply
  • how to set the choice column values if these are coming from different folder name in the document library?

    Reply
    • Here you go: This script creates a choice column with all folder names in the library as choices!

      #Parameters
      $SiteURL= "https://crescent.sharepoint.com/sites/pmo"
      $ListName = "ProjectList"
      $MetadataColumnName = "Project"
       
      #Connect to PnP Online
      Connect-PnPOnline -Url $SiteURL -Interactive
       
      #Get all folders from the library
      $List =  Get-PnPList -Identity $ListName
      $ListItems = Get-PnPListItem -List $ListName -PageSize 500
      $Folders = $ListItems | Where {$_.FileSystemObjectType -eq "Folder"}
      $FolderNames = @($Folders | ForEach-Object { $_.FieldValues.FileLeafRef})
       
      #Check if the column exists in the list
      $Field = Get-PnPField -Identity $MetadataColumnName -List $ListName -ErrorAction SilentlyContinue
      If($Field -eq $Null)
      {    
          #Create the Choice Metadata Column
          Add-PnPField -List $ListName -DisplayName $MetadataColumnName -InternalName $MetadataColumnName -Type Choice -AddToDefaultView -Choices $FolderNames | Out-Null
          Write-host "Column Created Successfully!" -f Green
      }
      Else
      {
          Write-host "Column Already Exists!" -f Yellow
      }
      
      Reply
  • SharePoint Online :how to set checkbox value from Powershell

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *