SharePoint Online: Get All Items from a List View using PowerShell

Requirement: Get All Items from a SharePoint Online List View in PowerShell.
sharepoint online powershell to get list items from view

PowerShell to Get List Items from a View 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"

#Config Variables
$SiteURL="https://crescenttech.sharepoint.com/"
$ListName="Projects"
$ViewName="Active Projects"

Try {
    $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 View
    $List = $Ctx.web.Lists.GetByTitle($ListName)
    $View = $List.Views.GetByTitle($ViewName)
    $Ctx.Load($View)
    $Ctx.ExecuteQuery()
 
    #Get all list items from the view
    $CAMLQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
    $CAMLQuery.ViewXml = $View.ViewQuery
    $ListItems = $List.GetItems($CAMLQuery)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    #Iterate throgh each item: 
    #ForEach($Item in $ListItems) { Write-host $Item[ColumnName]}
     
    Write-host "Total List Items Found in the Given View: $($ListItems.Count)" -ForegroundColor Green  
}
Catch {
    write-host -f Red "Error Getting List Items from the List View!" $_.Exception.Message
} 
SharePoint Online: Get All Items from a List View using PowerShell SharePoint Online: Get All Items from a List View using PowerShell Reviewed by Salaudeen Rajack on January 26, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.