
Hi @Anonymous
The codeproject article referenced in the question is apparently using either an incorrect or outdated schema for the CAML.
When you set the ViewXml
property of the CamlQuery
, the top-level XML element should be the <View>
. The <Query>
is one of the optional child elements. SharePoint was effectively ignoring your <RowLimit>
element (rather than perhaps throwing a helpful exception about XML schema), thus resulting in the List View Threshold exception.
We can fetch all the items(include folders) and keep it in collection object and query this collection object using Linq expression. Please refer to following script
#Get All Items from the List
$ListItems = Get-PnPListItem -List $ListName -Query "<View Scope='RecursiveAll'><RowLimit>5000</RowLimit></View>"
ForEach ($Item in $ListItems)
{
Exclude 'Folder' List Items
If($Item.FileSystemObjectType = 0){
#Get File Versions
$File = $Item.File
$Versions = $File.Versions
$Ctx.Load($File)
$Ctx.Load($Versions)
$Ctx.ExecuteQuery()
Write-host -f Yellow "Scanning File:"$File.Name
$VersionsCount = $Versions.Count
$VersionsToDelete = $VersionsCount - $VersionsToKeep
If($VersionsToDelete -gt 0)
{
write-host -f Cyan "`t Total Number of Versions of the File:" $VersionsCount
#Delete versions
For($i=0; $i -lt $VersionsToDelete; $i++)
{
write-host -f Cyan "`t Deleting Version:" $Versions[0].VersionLabel
$Versions[0].DeleteObject()
}
$Ctx.ExecuteQuery()
Write-Host -f Green "`t Version History is cleaned for the File:"$File.Name
}
}
}
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.