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
$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)
    #Get all list items from the view
    $CAMLQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
    $CAMLQuery.ViewXml = $View.ViewQuery
    $ListItems = $List.GetItems($CAMLQuery)

    #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

1 comment:

  1. I have to make a change for it to work for me as with above code it was giving me all items instead of just view items.

    $Query.ViewXml = $View.ListViewXml instead of $Query.ViewXml = $View.ViewQuery
    then I have looped thru the items.

    Thanks for the direction.


Please Login and comment to get your questions answered!

Powered by Blogger.