none
Adding a SharePoint Permission Groups to all folders in Library RRS feed

  • Question

  • We are using Office 365/SharePoint Online. There is a Document Library with many folders/sub folders and documents. Many folders in the library have broken inheritance that occurred as a result of sharing. Many of such folders are missing the library permission group.

    Is there a way to add a library level SharePoint Permission Group to all folders (talking about 1000s) even if they have broken inheritance.

    Thanks!

    Mayank Shyam

    Thursday, July 11, 2019 7:21 PM

All replies

  • Hi Mayank,

    what you need to do is Delete Unique Permissions and Restore Parent Permissions in a List.

    Below code does that for you. It deletes unique permissions of items and provide parent permissions of list.

    #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
    Thanks & Regards,


    sharath aluri

    Thursday, July 11, 2019 7:33 PM
  • Hi Mayank,

    Try the below script to add a group to all folders in a library.

    $webURL="https://tenant.sharepoint.com/sites/michael"  
    $lib="doc" 
    $UserName="michael"
    $Password ="password"
    $GroupName="test"
    $PermissionLevel="Edit"
     
    #Setup Credentials to connect
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
    $context = New-Object Microsoft.SharePoint.Client.ClientContext $webUrl
    $context.Credentials = $Credentials
    
    $Web = $context.web
    
    $folders = $Web.GetFolderByServerRelativeUrl($lib).folders
    
    $context.load($folders)
    $context.ExecuteQuery()
    
    foreach($folder in $folders){
    $context.load($folder)
    write-host $folder.name
    $Group =$Web.SiteGroups.GetByName($GroupName)
    $context.load($Group)
    $context.ExecuteQuery()
    $Role = $web.RoleDefinitions.GetByName($PermissionLevel)
    $RoleDB = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($context)
    $RoleDB.Add($Role)
    
    $GroupPermissions = $folder.ListItemAllFields.RoleAssignments.Add($Group,$RoleDB)
    $folder.Update()
    $context.ExecuteQuery()
    }
    

    Best Regards,

    Michael Han


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Friday, July 12, 2019 9:15 AM
  • Hi,

    Is there anything to update? 

    Please remember to mark the reply as an answer if it helps. 

    Have a nice day!

    Best Regards, 

    Michael Han 



    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Friday, July 19, 2019 9:29 AM