I have a document library with a large amount of directories and thus permissions which I would like to break inheritance of and have come across the SharePointDiary script which works perfectly apart from the SharePoint implemented list threshold limit.
To work around this I have been moving files/directories, running said script in short batches and then moving data back. However, that being said, sometimes my operation will time out. Could someone walk me through altering the script below to either run in batches to circumvent the list threshold limit or stop the operation from timing out?
TYIA!
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"
#Variables for Processing
$SiteURL = "https://contoso.sharepoint.com/site/"
$ListName = "DOCUMENTS"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the List
$List=$Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#Get All List Items
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml ="<View Scope='RecursiveAll' />"
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
#Loop through each list item
For($i=0;$i -lt $ListItems.Count;$i++)
{
#Break Inheritance copying permissions from parent
$ListItems[$i].BreakRoleInheritance($True, $False)
}
$Ctx.ExecuteQuery()
write-host -f Green "Permission Inheritance Broken for All Items in the List '$ListName'"
}
Catch {
write-host -f Red "Error:" $_.Exception.Message```