SharePoint Online: Update Choices in the Choice Field using PowerShell

Requirement: Update choice field choices in SharePoint Online using PowerShell

sharepoint online update choice field using powershell

SharePoint Online: PowerShell to Update Choice Field

As an administrator, you may need to update the choice field in the SharePoint Online list. Updating the choice field’s options can be done through the web browser user interface, but if you want to automate the process, PowerShell provides a way to do that. In this post, we’ll show you how to use PowerShell to update choices in a choice field.

#Load SharePoint Online 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"

##Variables for Processing
$SiteUrl = "https://Crescent.sharepoint.com"
$ListName= "Projects"
$FieldName="Category"
#Define Choices
$Choices = @("Growth Capital","Private Investment","Development", "Start up", "Real Estate")
  
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
#Set up 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()

#Get the field
$Field = $List.Fields.GetByInternalNameOrTitle($FieldName)
$Ctx.Load($Field)
$Ctx.ExecuteQuery()

#Cast the field to Choice Field
$ChoiceField = New-Object Microsoft.SharePoint.Client.FieldMultiChoice($Ctx, $Field.Path)
$Ctx.Load($ChoiceField)
$Ctx.ExecuteQuery()

#$choiceField.Choices.Clear()
$ChoiceField.Choices = $Choices            
$ChoiceField.UpdateAndPushChanges($True)
$Ctx.ExecuteQuery()

Write-host "Choice Field has been Updated!" -ForegroundColor Green

If you want to append to existing choices, use:

$ChoiceField.Choices += $Choices  

This adds value to the choice field’s existing choices.

PnP PowerShell to Add Choice to a Choice Column in SharePoint Online

How to get all choices of a choice field in the SharePoint Online list?

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName= "Projects"
$FieldInternalName = "Status"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get the Field
$Field = Get-PnPField -Identity $FieldInternalName -List $ListName

#get all choices of the choice field 
$Field.TypedObject.Choices

Now, Let’s update the choice field using PnP PowerShell:

#Parameter
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName= "Projects"
$FieldInternalName = "Status"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
$Ctx = Get-PnPContext

#Get the Field to update
$Field = Get-PnPField -Identity $FieldInternalName -List $ListName

#Cast the field to Choice Field
$ChoiceField = New-Object Microsoft.SharePoint.Client.FieldChoice($Ctx, $Field.Path)
$Ctx.Load($ChoiceField)
Invoke-PnPQuery

#Add a Choice Value
$ChoiceField.Choices += "Dropped"
$ChoiceField.UpdateAndPushChanges($True)
Invoke-PnPQuery

Here is another post on getting and setting choice field values using PowerShell: How to Read/Write Choice Field Values using PowerShell?

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

One thought on “SharePoint Online: Update Choices in the Choice Field using PowerShell

  • How can i delete Values in the Choices?

    Thanks

    Reply

Leave a Reply

Your email address will not be published.