SharePoint Online: PowerShell to Read List Items

Requirement: PowerShell to read list items in SharePoint Online

SharePoint Online: PowerShell to Read List Items
You can read SharePoint Online list with PowerShell in these three simple steps:

Step 1: Connect to SharePoint Online Site
To start with, we need the connection context to the SharePoint Online site.  
#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"
   
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/"
  
#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)

Step 2: Get List Items from SharePoint Online List
Once the connection is established, we can read or update any object from the SharePoint Online site. Here is how to get data from SharePoint Online list using PowerShell.
#List Name Variable
$ListName = "Projects"
 
#Get the list
$List = $Ctx.Web.Lists.GetByTitle($ListName)
 
#Read All Items from the list
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
Write-host "Total Number of Items Found in the List:"$ListItems.Count

Step 3: Loop through List Items Collection to Read List Items
Once we get the list items collection, we can read list items by iterating through each list item. 
#Iterate through List Items
ForEach($Item in $ListItems)
{
    #sharepoint online powershell read list items 
    Write-Host ("List Item ID:{0} - Title:{1}" -f $Item["ID"], $Item["Title"])
}

PowerShell to Read SharePoint Online List 
Here is the complete PowerShell script to read SharePoint Online List Items:
#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"
   
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/"
$ListName = "Projects"
  
#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
$List = $Ctx.Web.Lists.GetByTitle($ListName)
 
#sharepoint online powershell read list items
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
Write-host "Total Number of Items Found in the List:"$ListItems.Count

#Iterate through List Items
ForEach($Item in $ListItems)
{
    #sharepoint online powershell read list items 
    Write-Host ("List Item ID:{0} - Title:{1}" -f $Item["ID"], $Item["Title"])
}
If you have a large list with >5000 items, You may have to batch process list items to avoid throttling issue: The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator." PowerShell to Get All List Items from Large Lists ( >5000 Items) in SharePoint Online
SharePoint Online: PowerShell to Read List Items SharePoint Online: PowerShell to Read List Items Reviewed by Salaudeen Rajack on January 01, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.