SharePoint Online: Get All Items from a List View using PowerShell
Requirement: Get All Items from a SharePoint Online List View in PowerShell.
PowerShell to Get List Items from a View in SharePoint Online:
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 }
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.
ReplyDelete$Query.ViewXml = $View.ListViewXml instead of $Query.ViewXml = $View.ViewQuery
then I have looped thru the items.
Thanks for the direction.