SharePoint Online: Get-Set Choice Field Value using PowerShell

Requirement: Get / Set Choice Field Value in SharePoint Online using PowerShell.

sharepoint online get set choice field value using PowerShell

PowerShell to Get Choice Field Value from a List Item in SharePoint Online:

This blog post will explore how to get or set the value of a choice field in SharePoint Online using PowerShell. We will cover how to use PowerShell to connect to SharePoint Online, and then we will look at some code examples that can be used to retrieve or update the value of a choice column of list items.

Here is the PowerShell script to read choice column values in SharePoint Online:

#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"

#Parameters
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"
$FieldName = "Project_x0020_Status" #Internal Name of the Field
$ItemID = 10

#Get credentials to connect
$Cred = Get-Credential

Try {
    #Setup the Context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName, $Cred.Password)

    #Get the List Item
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $ListItem = $List.GetItemById($ItemID)
    $Ctx.Load($ListItem)
    $Ctx.ExecuteQuery()

    #Get the value of Choice Field
    Write-Host $ListItem[$FieldName]
    }
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

Get Multi-Choice Column Value using CSOM PowerShell

To retrieve the values from a Choice column that’s allowed to accept multiple choices, use this PowerShell script:

#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"

#Parameters
$SiteURL = "https://crescent.sharepoint.com"
$ListName ="Projects"
$FieldName = "Category"
$ItemID = 10

#Get credentials to connect
$Cred = Get-Credential

Try {
    #Setup the Context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName, $Cred.Password)

    #Get the List Item to update
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $ListItem = $List.GetItemById($ItemID)
    $Ctx.Load($ListItem)
    $Ctx.ExecuteQuery()

    #Get Multiple Choices Field Value from the List Item
    $FieldValues = $ListItem[$FieldName]

    #Print Each Value Selected
    ForEach($Value in $FieldValues)
    {
        Write-host $Value
    }
    }
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

Set Choice Field Value in SharePoint Online using PowerShell

Here is the PowerShell to update the choice field value of a list item:

#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"

#Parameters
$SiteURL = "https://crescent.sharepoint.com"
$ListName ="Projects"
$FieldName = "Project_x0020_Status"
$ItemID = 10

#Get credentials to connect
$Cred = Get-Credential

Try {
    #Setup the Context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName, $Cred.Password)

    #Get the List Item to update
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $ListItem = $List.GetItemById($ItemID)
    $Ctx.Load($ListItem)
    $Ctx.ExecuteQuery()

    #Update the value of Choice Field in the List Item
    $ListItem[$FieldName] = "Active"
    $ListItem.Update()
    $Ctx.ExecuteQuery()
    Write-host -f Green "Choice Field Value has been updated for the list Item!"
    }
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

CSOM PowerShell Script to Update Multi-Choice Field Value

Similarly, to set the value for a Choice field that allows multiple choices, use this PowerShell:

#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"

#Parameters
$SiteURL = "https://crescent.sharepoint.com"
$ListName ="Projects"
$FieldName = "Category"
$ItemID = 10

#Get credentials to connect
$Cred = Get-Credential

Try {
    #Setup the Context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName, $Cred.Password)

    #Get the List Item to update
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $ListItem = $List.GetItemById($ItemID)
    $Ctx.Load($ListItem)
    $Ctx.ExecuteQuery()

    #Update the value of Multiple Choices Field in the List Item
    $ListItem[$FieldName] = @("Development", "IT Applications", "Real Estate")
    $ListItem.Update()
    $Ctx.ExecuteQuery()
    Write-host -f Green "Choice Field Value has been updated for the list Item!"
    }
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

PnP PowerShell to Update Choice Field Value:

To update choice field values using PnP PowerShell, use:

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$ItemID = 1

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Update choice column value using PowerShell
Set-PnPListItem -List $ListName -Identity $ItemID -Values @{"Status"= "Completed"}

This script sets the “Status” field value to “Completed”. Similarly, for Multiple choices, separate them with “;#” (semicolon and # symbol). E.g.,

Set-PnPListItem -List $ListName -Identity $ItemID -Values @{"Industries"= "Aerospace;#Automation;#Energy"}

Wrapping up

In summary, using PowerShell to retrieve or update the value of a choice field in SharePoint Online is an efficient way to access and manipulate data stored in SharePoint Online. By using the scripts provided above, you can quickly and easily retrieve the value of a choice field, change it, and save the new value in SharePoint Online list items.

If you want to update the available choices in the choice field, use: PowerShell to Update the Choices in the Choice Field in SharePoint Online

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

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