Question

I have a problem with my Sharepoint. I migrated data from NAS to SPO using a migrate tool. This solution adds unique permission to the library and sublibraries. I have more than 250k objects so I can't use web site to delete this, I tried to use this method Remove unique permissions on all locations on site SharePoint 2013

I Changed the start function from Get-SPWeb to Get-SPOService.

this does not work. Could you help me, how can I delete unique permission in one library on one site in Sharepoint online when is it more than 5000 objects?

Was it helpful?

Solution

You could try the below powershell script:

$ListName = "Documents"
Connect-PnPOnline -Url https://yoursite
#Get all list items in batches
$ListItems = Get-PnPListItem -List $ListName -PageSize 500
#Iterate through each list item
ForEach($ListItem in $ListItems)
{
    #Check if the Item has unique permissions
    $HasUniquePermissions = Get-PnPProperty -ClientObject $ListItem -Property "HasUniqueRoleAssignments"
    If($HasUniquePermissions)
    {       
      Set-PnPListItemPermission -List $ListName -Identity $ListItem.ID -InheritPermissions

    }
}

Reference:

https://www.sharepointdiary.com/2016/02/powershell-to-delete-unique-permissions-for-all-list-items-sharepoint-online.html

OTHER TIPS

You could try the below PowerShell script:

#Load SharePoint CSOM 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"

#Function to remove unique permissions and inherit from the parent
Function Remove-ListUniquePermissions
{
param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName
    )

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #sharepoint online powershell inherit permissions
        $List=$Ctx.Web.Lists.GetByTitle($ListName)
        $List.ResetRoleInheritance()
        $List.Update()
        $Ctx.ExecuteQuery()

        Write-Host "Unique Permissions are removed and inherited from the Parent!" -ForegroundColor Green
    }

    Catch {
        write-host -f Red "Error Deleting Unique Permissions!" $_.Exception.Message
    }
}
#sharepoint online powershell reset permissions
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"

#Call the function to remove unique permissions from a list
Remove-ListUniquePermissions -SiteURL $SiteURL -ListName $ListName

Note:

For more than 5000 items condition, you need to load the items with a row limit 5000 - if the result gives more than 5k then need to call the above function Remove-ListUniquePermission

Example of how to call the above function Remove-ListUniquePermission

$list = $ctx.Web.Lists.GetByTitle($DocLibName)
$ctx.Load($list)
$ctx.ExecuteQuery()
## View XML
$camlQuery= @"
<View Scope="RecursiveAll">
    <Query>
        <OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
    </Query>
    <RowLimit Paged="TRUE">5000</RowLimit>
</View>
"@

$currentCollection = $list.GetItems($camlQuery)
$ctx.Load($currentCollection)
$ctx.ExecuteQuery()

if ($currentCollection -eq 5000)
{
   Remove-ListUniquePermissions -SiteURL $SiteURL -ListName $ListName

}

Source:

Delete Unique Permissions on a List or Library in SharePoint Online using PowerShell:

Get All Items in 5000+ large list with CSOM in PowerShell

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top