Skip to content

Instantly share code, notes, and snippets.

@pmatthews05
Created March 2, 2021 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmatthews05/3b2246f7349a4be3ff8a4b63bbc6346f to your computer and use it in GitHub Desktop.
Save pmatthews05/3b2246f7349a4be3ff8a4b63bbc6346f to your computer and use it in GitHub Desktop.
Getting all Items from PNP.Powershell using version 1.2.0
<#
.SYNOPSIS
Need to use PowerShell version 7.1
Need to use the new PNP.PowerShell "Install-Module PnP.Powershell -MaximumVersion 1.2.0 -Scope:CurrentUser -Force"
Loops through Library obtaining all items.
You need to connect to the site first using PNP
Connect-PnPOnline -url:https://beisgov.sharepoint.com/sites/[siteURL] -PnPManagementShell
.EXAMPLE
To get all Files/Folder and pump to a CSV.
.\Get-AllItems.ps1 -ListName:"Documents" | Out-File .\libraryOutput.json
#>
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory = $true)]
[string]$ListName
)
function Get-ListDataAsStream {
param (
[Parameter(Mandatory)]
[string]
$SiteUrl,
[Parameter(Mandatory)]
[string]
$ListName,
[Parameter(Mandatory)]
[string]
$CAMLQuery,
[Parameter(Mandatory = $false)]
[string]
$NextLink
)
$QueryUrl = "$SiteUrl/_api/web/lists/GetbyTitle('$ListName')/RenderListDataAsStream"
Write-Information -MessageData:"QueryUrl:$QueryUrl"
if ($NextLink -and $NextLink.StartsWith('?')) {
$NextLink = $NextLink.Substring(1)
Write-Information -MessageData:"Paging:$NextLink"
}
$body = @{
parameters = @{
Paging = $NextLink
RenderOptions = 2
ViewXml = $CAMLQuery
}
}
return Invoke-PnPSPRestMethod -Url:"$QueryUrl" -Method:Post -ContentType:"application/json;odata=nometadata" -Content:$body
}
function get-CAMLQuery {
$CAMLQuery = "<View Scope='RecursiveAll'>
<Query>
<OrderBy>
<FieldRef Name='Modified' Ascending='TRUE'/>
</OrderBy>
</Query>
<RowLimit Paged='TRUE'>1000</RowLimit>
</View>"
return $CAMLQuery
}
function Get-OutputInformation {
param(
[Parameter(Mandatory)]
[string]
$SiteUrl,
[Parameter(Mandatory)]
[string]
$ListTitle,
[Parameter(Mandatory)]
[string]
$Items
)
$ItemArray = $Items | ConvertFrom-Json
Write-Information -MessageData "Processing $($ItemArray.Count) items..."
#Here you would loop round ItemArray and grab what you want.
Write-Output $Items
}
$ErrorActionPreference = 'Stop'
$InformationPreference = 'Continue'
$WarningPreference = 'Continue'
$library = Get-PnPList -Identity:$ListName
if ($null -eq $library) {
throw "Library does not exist"
}
$web = Get-PnPWeb
$webServerRelatedUrl = $web.ServerRelativeUrl
$webUrl = $web.url
$listTitle = $library.Title
$CAMLQuery = get-CAMLQuery
$startTime = Get-Date
do {
$results = Get-ListDataAsStream -SiteUrl:$webUrl -ListName:$ListName -CAMLQuery:$CAMLQuery -NextLink:$nextLink
Get-OutputInformation -SiteUrl:$webServerRelatedUrl -ListTitle:$listTitle -Items:$results.Row.ToString()
$nextLink = ""
if ($results.ContainsKey("NextHref")) {
$nextLink = $results.NextHref.ToString()
}
}while ("" -ne $nextLink)
$endTime = Get-Date
$timeTaken = New-TimeSpan -Start $startTime -End $endTime
Write-Information -MessageData:"Time taken to process library $($timeTaken -f 'c') "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment