SharePoint Online: CAML Query for Choice Field in PowerShell

Requirement: CAML Query for Choice Field in SharePoint Online using PowerShell.

Get List Items Where a Choice Field is Equal to Particular Value:

Let’s get all projects from projects list where the “Priority” choice field is “Medium”

#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"
#Set parameter values
#Get Credentials to connect
$Cred= Get-Credential
#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 Object
$List = $Ctx.Web.lists.GetByTitle($ListName)

#Define the CAML Query - sharepoint caml query choice field
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View Scope='RecursiveAll'>
                <FieldRef Name='Priority' /><Value Type='Choice'>(3) High</Value>

#Get All List Items matching the query
$ListItems = $List.GetItems($Query)

Write-host "Total Number of Items:"$ListItems.count

#Loop through each List Item
ForEach($Item in $ListItems)
    #Do something
    Write-host $Item["Title"]

SharePoint CAML for Multiple Choice

Let’s get all projects where the priority multichoice column has both “High” and “Medium” values selected.

$Query.ViewXml = "@
<View Scope='RecursiveAll'>
                    <FieldRef Name='Priority' /><Value Type='MultiChoice'>High</Value>
                    <FieldRef Name='Priority' /><Value Type='MultiChoice'>Medium</Value>

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply