kwizcom banner advertisement

SharePoint Online: PowerShell to Loop Through List Items

Requirement: SharePoint Online PowerShell to Loop through List Items

Make sure you have SharePoint Online Client SDK downloaded and installed in your machine before running the script: https://www.microsoft.com/en-us/download/details.aspx?id=42038

PowerShell to Loop through List Items in SharePoint Online
Here is a basic example for iterating through each list item in a SharePoint Online list or library.
#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"
   
#Config Parameters
$SiteURL="https://crescenttech.sharepoint.com"
$ListName="Projects"
  
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
   
#Get the List
$List=$Ctx.Web.Lists.GetByTitle($ListName)

#Get All List items
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()

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

#Loop through all list items
Foreach ($Item in $ListItems)
{
    #Get the List Item's Title
    Write-host $Item["Title"]
}
This PowerShell script gets all items from the given list, loop through each item and gets the list item's title. Similarly, you can loop through each document in a document library using PowerShell: SharePoint Online: PowerShell to Iterate Through All Files in a Document Library
SharePoint Online: PowerShell to Loop Through List Items SharePoint Online: PowerShell to Loop Through List Items Reviewed by Salaudeen Rajack on April 19, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.