Forum Discussion
Fromelard
Oct 14, 2020Iron Contributor
SharePoint - How to Reset Inheritance Permission set into an SP DocLib folder or file
When we are importing File Server content into SharePoint using dedicated tools for, we can import permission set configured at the sub levels (subfolders or documents). That import can create some i...
roymaalouf
Oct 15, 2021MCT
I run the script as below:
install-module SharePointPnPPowerShellOnline -Force #to install that module the first time only
#install-module SharePointPnPPowerShellOnline -Force #to install that module the first time only
Write-Host " ---------------------------------------------- "
Import-Module SharePointPnPPowerShellOnline
Write-Host " ---------------------------------------------- "
#Config Variables
$SiteURL = " https://medair.sharepoint.com/sites/GlobalITS/"
$ListTitle = "Documents"
$foldertoscope = "/sites/GlobalITS/Shared%20Documents/"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
$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()
}
}
----------------------------------------------
WARNING: The names of some imported commands from the module 'SharePointPnPPowerShellOnline' include unapproved verbs t
hat might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again
with the Verbose parameter. For a list of approved verbs, type Get-Verb.
----------------------------------------------
WARNING:
You are running the legacy version of PnP PowerShell.
This version will be archived soon which means that while staying available, no updates or fixes will be released.
Consider installing the newer prereleased cross-platform version of PnP PowerShell.
This version has numerous improvements and many more cmdlets available.
To install the new version:
Uninstall-Module -Name SharePointPnPPowerShellOnline -AllVersions -Force
Install-Module -Name PnP.PowerShell
Read more about the new cross-platform version of PnP PowerShell at
https://pnp.github.io/powershell
To hide this message set the environment variable PNPLEGACYMESSAGE to "false"
In PowerShell add $env:PNPLEGACYMESSAGE='false' to your profile. Alternatively use 'Connect-PnPOnline -Url [yoururl] -W
arningAction Ignore'
Cannot convert argument "query", with value: "Microsoft.SharePoint.Client.CamlQuery", for "GetItems" to type
"Microsoft.SharePoint.Client.CamlQuery": "Cannot convert the "Microsoft.SharePoint.Client.CamlQuery" value of type
"Microsoft.SharePoint.Client.CamlQuery" to type "Microsoft.SharePoint.Client.CamlQuery"."
At line:45 char:5
+ $currentCollection = $ll.GetItems($camlQuery)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
Cannot find an overload for "Load" and the argument count: "1".
At line:46 char:5
+ $ctx.Load($currentCollection)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Collecting items. Current number of items: 1
Total number of items: 1
Cannot index into a null array.
At line:63 char:8
+ if($allItems[$j]["FileRef"].StartsWith($foldertoscope))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
I got the following result:
install-module SharePointPnPPowerShellOnline -Force #to install that module the first time only
#install-module SharePointPnPPowerShellOnline -Force #to install that module the first time only
Write-Host " ---------------------------------------------- "
Import-Module SharePointPnPPowerShellOnline
Write-Host " ---------------------------------------------- "
#Config Variables
$SiteURL = " https://medair.sharepoint.com/sites/GlobalITS/"
$ListTitle = "Documents"
$foldertoscope = "/sites/GlobalITS/Shared%20Documents/"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
$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()
}
}
----------------------------------------------
WARNING: The names of some imported commands from the module 'SharePointPnPPowerShellOnline' include unapproved verbs t
hat might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again
with the Verbose parameter. For a list of approved verbs, type Get-Verb.
----------------------------------------------
WARNING:
You are running the legacy version of PnP PowerShell.
This version will be archived soon which means that while staying available, no updates or fixes will be released.
Consider installing the newer prereleased cross-platform version of PnP PowerShell.
This version has numerous improvements and many more cmdlets available.
To install the new version:
Uninstall-Module -Name SharePointPnPPowerShellOnline -AllVersions -Force
Install-Module -Name PnP.PowerShell
Read more about the new cross-platform version of PnP PowerShell at
https://pnp.github.io/powershell
To hide this message set the environment variable PNPLEGACYMESSAGE to "false"
In PowerShell add $env:PNPLEGACYMESSAGE='false' to your profile. Alternatively use 'Connect-PnPOnline -Url [yoururl] -W
arningAction Ignore'
Cannot convert argument "query", with value: "Microsoft.SharePoint.Client.CamlQuery", for "GetItems" to type
"Microsoft.SharePoint.Client.CamlQuery": "Cannot convert the "Microsoft.SharePoint.Client.CamlQuery" value of type
"Microsoft.SharePoint.Client.CamlQuery" to type "Microsoft.SharePoint.Client.CamlQuery"."
At line:45 char:5
+ $currentCollection = $ll.GetItems($camlQuery)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
Cannot find an overload for "Load" and the argument count: "1".
At line:46 char:5
+ $ctx.Load($currentCollection)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Collecting items. Current number of items: 1
Total number of items: 1
Cannot index into a null array.
At line:63 char:8
+ if($allItems[$j]["FileRef"].StartsWith($foldertoscope))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
I got the following result:
NickV2114
Feb 02, 2022Copper Contributor
Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (400) Bad Request."
At line:66 char:9
+ $ctx.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
We get this error - is this Microsoft Throttling our connection?
At line:66 char:9
+ $ctx.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
We get this error - is this Microsoft Throttling our connection?