Home Blog SharePoint Online Restore Inheritance for all items in the list

SharePoint Online Restore Inheritance for all items in the list

Alex Pollard 31 Jul 2023

Visit our website to find out more useful solutions and services from Chiron IT https://chironit.com

Here is the solution how in SharePoint Online Restore Inheritance for all items in the list or files or documents for a specific list or library in SharePoint online


Start Powershell as Admin and launch .ps1 script with the next command:

powershell -ExecutionPolicy ByPass -File script.ps1

Solution #1

#Load SharePoint Online Assemblies
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"
#To call a non-generic method Load
Function Invoke-LoadMethod() {
            [Microsoft.SharePoint.Client.ClientObject]$Object = $(throw "Please provide a Client Object"),
   $ctx = $Object.Context
   $load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load") 
   $type = $Object.GetType()
   $clientLoad = $load.MakeGenericMethod($type)
   $Parameter = [System.Linq.Expressions.Expression]::Parameter(($type), $type.Name)
   $Expression = [System.Linq.Expressions.Expression]::Lambda([System.Linq.Expressions.Expression]::Convert([System.Linq.Expressions.Expression]::PropertyOrField($Parameter,$PropertyName),[System.Object] ), $($Parameter))
   $ExpressionArray = [System.Array]::CreateInstance($Expression.GetType(), 1)
   $ExpressionArray.SetValue($Expression, 0)
##Variables for Processing
$SiteURL = "https://example.sharepoint.com/properties/sdrive"
$ListName ="Documents"
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
$Context.Credentials = $Credentials
#Get the List
$List = $Context.web.Lists.GetByTitle($ListName)
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "50000"
#Batch process list items - to mitigate list threashold issue on larger lists
Do {  
    #Get items from the list in batches
    $ListItems = $List.GetItems($Query)
    $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
    #Loop through each List item
    ForEach($ListItem in $ListItems)
        Invoke-LoadMethod -Object $ListItem -PropertyName "HasUniqueRoleAssignments"
        if ($ListItem.HasUniqueRoleAssignments -eq $true)
            #Reset Permission Inheritance
            Write-host  -ForegroundColor Yellow "Inheritence Restored on Item:" $ListItem.ID
} While ($Query.ListItemCollectionPosition -ne $null)
Write-host "Broken Permissions are Deleted on All Items!" -ForegroundColor Green

Solution #2

function Restore-SPOListAllItemsInheritance
   param (
  $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
  $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)

  ## View XML
$qCommand = @"


## Page Position
$position = $null
## All Items
$allItems = @()
    $camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
    $camlQuery.ListItemCollectionPosition = $position
    $camlQuery.ViewXml = $qCommand
 ## Executing the query
    $currentCollection = $ll.GetItems($camlQuery)
 ## 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++)
      Write-Host "Resetting permissions for " $allItems[$j]["Title"] ".." $allItems[$j]["FileRef"]

# Paths to SDK. Please verify location on your computer.
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"

#Enter the data
$AdminPassword=Read-Host -Prompt "Enter password" -AsSecureString

Restore-SPOListAllItemsInheritance -Username $username -Url $Url -AdminPassword $AdminPassword -ListTitle $ListTitle

Leave a Reply

Your email address will not be published. Required fields are marked *

Thank you for your comment

Your comment has been successfully submitted and will be published immediately after moderation.

Have a great day!

Have questions?

    Contact me

    Thank you for reaching out!

    Your message has been successfully delivered. I will be in touch with you shortly to discuss your inquiry further.

    If you have any urgent matters or need immediate assistance, please don't hesitate to contact me directly at my email or phone. I look forward to connecting with you and assisting you with your needs.

    Have a great day!
    Contact Information
    This website uses cookies

    This website uses cookies. By continuing, I assume your permission to deploy cookies as detailed in the Privacy Policy.