how to remove Sharepoint folder's permissions without impacting the sub folders permissions using c#

Occasional Contributor

Hi,

 

I have tried to remove all the folder's permissions using this piece of code :

 

 

Folder clientFolder = ctx.Web.GetFolderByServerRelativeUrl(spClientFolderUrl);
ctx.Load(clientFolder, f => f.Name, f => f.ListItemAllFields, f => f.ListItemAllFields.HasUniqueRoleAssignments);
ctx.ExecuteQueryRetry();

if (!clientFolder.ListItemAllFields.HasUniqueRoleAssignments)
{
    clientFolder.ListItemAllFields.BreakRoleInheritance(true, true);
    ctx.ExecuteQuery();
    Log.Info("INFO --- folder permission inheritance broken on the sharepoint client folder");
};

ctx.Load(clientFolder, f => f.Name, f => f.ListItemAllFields, f => f.ListItemAllFields.HasUniqueRoleAssignments,
        f => f.ListItemAllFields.RoleAssignments.Include(r => r.Member, r => r.Member.LoginName, r => r.Member.PrincipalType,
        r => r.RoleDefinitionBindings.Include(roleDef => roleDef.Name, roleDef => roleDef.Description)));
ctx.ExecuteQueryRetry();

RoleAssignmentCollection roleAssignments = clientFolder.ListItemAllFields.RoleAssignments;

foreach (RoleAssignment roleAssignment in roleAssignments.ToList())
{

        roleAssignment.DeleteObject();
        ctx.ExecuteQuery();
        Log.Info("INFO --- permission removed for: " + roleAssignment.Member.LoginName);
    
}

 

 

That worked but all permissions of the sub folders have also been removed.

How can I remove all permissions of a folder without impacting the sub folders permissions.

 

Thanks

 

Arioule

0 Replies