Forum Discussion
SharePoint - How to Reset Inheritance Permission set into an SP DocLib folder or file
https://<domain>.sharepoint.com/Shared%20Documents
however...when it runs, the collecting occurs but the resetting does not. I am guessing I do not have a variable correct. Do you have a suggestion?
- OsmundoJun 15, 2022Copper ContributorI have the same issue with this script, it stops at item collection and never runs the actual resetting. Anybody found the solution?
- AdamDoddsMar 09, 2023Copper Contributor
I know you asked well over 6 months ago but if anyone else faces the same problem, here is how I fixed it
For a root SharePoint site it should be set out like this:
#Config Variables
$SiteURL = "https://company.sharepoint.com/"
$ListTitle = "Documents"$foldertoscope = "/Shared Documents/"
- jrush56Sep 24, 2022Copper Contributor
Hello I have seen a couple comments that it is not resetting but only counting or doing "item collection"
I am not sure if it was just a formatting issue but I have gotten the current script to work. I have set the variables to a default non-usable value. Please edit them to fit your environment
#install-module SharePointPnPPowerShellOnline -Force #to install that module the first time only
Write-Host " ---------------------------------------------- "
Import-Module PnP.PowerShell
Write-Host " ---------------------------------------------- "
#Config Variables
$SiteURL = "https://domain.sharepoint.com/sites/SiteName"
$ListTitle = "Documents"
#For some libraries it will be Shared%20Documents. Be sure to retain the slash before /sites/SiteName/Documents
$foldertoscope = "/sites/SiteName/Documents"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
$ctx = Get-PnPContext
$ctx.Load($ctx.Web.Lists)
$ctx.Load($ctx.Web)
$ctx.Load($ctx.Web.Webs)
$ctx.ExecuteQuery()
$ll=$ctx.Web.Lists.GetByTitle($ListTitle)
$ctx.Load($ll)
$ctx.ExecuteQuery()
## View XML
$qCommand = @"
<View Scope="RecursiveAll">
<Query>
<OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
</Query>
<RowLimit Paged="TRUE">5000</RowLimit>
</View>
"@
## Page Position
$position = $null
## All Items
$allItems = @()
Do{
$camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$camlQuery.ListItemCollectionPosition = $position
$camlQuery.ViewXml = $qCommand
## Executing the query
$currentCollection = $ll.GetItems($camlQuery)
$ctx.Load($currentCollection)
$ctx.ExecuteQuery()
## Getting the position of the previous page
$position = $currentCollection.ListItemCollectionPosition
# Adding current collection to the allItems collection
$allItems += $currentCollection
Write-Host "Collecting items. Current number of items: " $allItems.Count
}
while($position -ne $null)
Write-Host "Total number of items: " $allItems.Count
for($j=0;$j -lt $allItems.Count ;$j++)
{
if($allItems[$j]["FileRef"].StartsWith($foldertoscope))
{
Write-Host "Resetting permissions for " $allItems[$j]["Title"] ".." $allItems[$j]["FileRef"]
$allItems[$j].ResetRoleInheritance()
$ctx.ExecuteQuery()
}
}