SharePoint Online: PowerShell to Get List Item Field Value

Requirement: PowerShell to Get List Item Field Value in SharePoint Online

SharePoint Online PowerShell to Get List Item 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 = "Manager"
$ItemId="1"

#Set Authentication Parameters
$UserName = "Salaudeen@TheCrescentTech.com"
$Password = "Password1"
$SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force

#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
 
#Get List Item
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ItemId)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()    

#Get the Field Value
$ListItem[$FieldName]

#Also does the same: Get the Field Value
#$ListItem.FieldValues[$FieldName]

Above script returns data as per the field type. E.g. as the "Manager" field is "Person or Lookup", this PowerShell script returns Microsoft.SharePoint.Client.FieldUserValue data type. Let's get only the text value from a field as:
#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 = "Manager"
$ItemId="1"

#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 List Item
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ItemId)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Get List Item Field Values as Text
$ListItemFieldValues = $ListItem.FieldValuesAsText
$Ctx.Load($ListItemFieldValues)
$Ctx.ExecuteQuery()

#Get the Text value of "Manager" field from the list Item
$ListItemFieldValues[$FieldName]

Here is my other PowerShell scripts to get data from list items in SharePoint Online:
  1. SharePoint Online: Get Yes/No Checkbox Field Value using PowerShell
  2. SharePoint Online: Get Lookup Field Value using PowerShell
  3. SharePoint Online: Get Person or group Field Value using PowerShell
SharePoint Online: PowerShell to Get List Item Field Value SharePoint Online: PowerShell to Get List Item Field Value Reviewed by Salaudeen Rajack on April 16, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.