0

I have a library that contains different projects. The projects are created based on a specific project structure format in the same library as a sample for the users using that library.

Currently, the owners of that library assign permissions to the users after the project is created manually.

I want to simplify the process of assigning permissions to the users and avoid manual assignments for the owners.

I thought about creating different project formats, assigning permissions to the formats based on my requirements, and asking the owners to simply copy/paste the formats and assign only the required metadata columns.

However, it seems that copy/paste does not take into consideration the permissions assigned for the format folders/subfolders structure and keeps inheriting permission from the parent library.

What is the best practice/solution to avoid the manual permission assignments for the projects based on different formats by using copy/paste commands? Can someone provide an example of whether the above is feasible?

Any help is highly appreciated.

Thank you!

1

You could try the code in the blog:

  #PowerShell Function to copy permissions between Folders in SharePoint Online
    Function Copy-PnPFolderPermissions
    {
        [cmdletbinding()]
         param(
             [Parameter(Mandatory=$True)] [string] $WebURL,
             [Parameter(Mandatory=$True)] [string] $SourceFolderURL,
             [Parameter(Mandatory=$True)] [string] $TargetFolderURL,
             [Parameter(Mandatory=$False)] [Bool] $AppendToExisting = $True
         )
        Try {
            #Connect to PnP Online
            Connect-PnPOnline -Url $WebURL -Interactive
 
        #Get the Web
        $Web = Get-PnPweb
        $Ctx = Get-PnPContext
 
        #Get Source and Target Folders
        $SourceFolderItem = Get-PnPFolder -Url $SourceFolderURL -Includes ListItemAllFields.HasUniqueRoleAssignments
        $SourceFolder = $SourceFolderItem.ListItemAllFields
        $TargetFolderItem = Get-PnPFolder -Url $TargetFolderURL -Includes ListItemAllFields.HasUniqueRoleAssignments
        $TargetFolder = $TargetFolderItem.ListItemAllFields
 
        #if permissions are Inherited in Target Folder, Break the Inheritance
        If(!$TargetFolder.HasUniqueRoleAssignments)
        {
            If($AppendToExisting -eq $True)
            {
                #Break Folder permissions - keep all existing permissions & Clear Item level permissions
                $TargetFolder.BreakRoleInheritance($True,$False)
            }
            else
            {
                $TargetFolder.BreakRoleInheritance($False,$False)
            }
        }
        Else #If the Folder has unique Permissions already
        {
            If($AppendToExisting -eq $False)
            {
                $TargetFolder.ResetRoleInheritance()
                $TargetFolder.BreakRoleInheritance($False,$False)
            }
        }
        Invoke-PnPQuery
 
        #Get all permissions assigned to the source folder
        $SourceRoleAssignments = Get-PnPProperty -ClientObject $SourceFolder -Property RoleAssignments
  
        #Copy Source Folder permissions to Destination Folder
        ForEach($RoleAssignment in $SourceRoleAssignments)
        {
            #Get RoleDefinitions of the Role Assignment
            Get-PnPProperty -ClientObject $RoleAssignment -Property RoleDefinitionBindings, Member
 
            #Leave the Hidden permissions
            If($RoleAssignment.Member.IsHiddenInUI -eq $False)
            {
                $SourcePermissions = $RoleAssignment.RoleDefinitionBindings | Where {$_.Name -notin("Limited Access")}
                $PermissionLevels = ($SourcePermissions | Select -ExpandProperty Name) -join "; "
 
                If($SourcePermissions -ne $null)
                {
                    #Add Source Folder's Permission Level to the Target Folder
                    $RoleDefBindings = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($Ctx)
                    ForEach($RoleDefinition in $SourcePermissions)
                    {
                        $RoleDefBindings.Add($RoleDefinition)
                    }
                    $Permissions = $TargetFolder.RoleAssignments.Add($RoleAssignment.Member,$RoleDefBindings)
                    $TargetFolder.Update()
                    Invoke-PnPQuery
                    Write-host "Copied '$($RoleAssignment.Member.Title)' with Permissions '$PermissionLevels'"
                }
            }
        }
    }
    Catch {
        write-host -f Red "Error Copying Folder Permissions!" $_.Exception.Message
    }
}
 
#Set Parameters
$WebURL = "https://crescent.sharepoint.com/sites/Marketing"
 
#Server Relative URLs of Source and Target Folders
$SourceFolderURL = "/sites/Marketing/Shared Documents/Old"
$TargetFolderURL = "/sites/Marketing/Shared Documents/New"
 
#Call the function to copy Folder permissions
Copy-PnPFolderPermissions -WebURL $WebURL -SourceFolderURL $SourceFolderURL -TargetFolderURL $TargetFolderURL

Reference:

How to Copy Permissions from One Folder to Another in SharePoint Online using PowerShell?

============Update=====================

Thanks for the reply. I have updated my answer in this thread in Q&A forum to help more people in the future:Copy permissions of a folder and subfolder in a library.

1
  • Hi Elsie, thank you for providing the example from the blog post. Does that mean every time they want to create a project in the library they have to ask an IT to execute the PowerShell script to copy the permissions for a folder to another? Is there no other way to do avoid that?
    – SmithBZ
    Nov 15 '21 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.