SharePoint Online: Get-Set Choice Field Value using PowerShell

Requirement: Get / Set Choice Field Value in SharePoint Online
sharepoint online get set choice field value using PowerShell
Here is my PowerShell scripts collection to read and update choice column values in SharePoint Online.

PowerShell to Get Choice Field Value from a List Item 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://crescenttech.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
#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://crescenttech.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
#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://crescenttech.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 fro the list Item!"
    }
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

CSOM PowerShell Script to Update Multi-Choice Field Value
#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://crescenttech.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
}
SharePoint Online: Get-Set Choice Field Value using PowerShell SharePoint Online: Get-Set Choice Field Value using PowerShell Reviewed by Salaudeen Rajack on June 22, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.