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

%3CLINGO-SUB%20id%3D%22lingo-sub-1772937%22%20slang%3D%22en-US%22%3Ehow%20to%20remove%20Sharepoint%20folder's%20permissions%20without%20impacting%20the%20sub%20folders%20permissions%20using%20c%23%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1772937%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EHi%2C%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EI%20have%20tried%20to%20remove%20all%20the%20folder's%20permissions%20using%20this%20piece%20of%20code%20%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3EFolder%20clientFolder%20%3D%20ctx.Web.GetFolderByServerRelativeUrl(spClientFolderUrl)%3B%0Actx.Load(clientFolder%2C%20f%20%3D%26gt%3B%20f.Name%2C%20f%20%3D%26gt%3B%20f.ListItemAllFields%2C%20f%20%3D%26gt%3B%20f.ListItemAllFields.HasUniqueRoleAssignments)%3B%0Actx.ExecuteQueryRetry()%3B%0A%0Aif%20(!clientFolder.ListItemAllFields.HasUniqueRoleAssignments)%0A%7B%0A%20%20%20%20clientFolder.ListItemAllFields.BreakRoleInheritance(true%2C%20true)%3B%0A%20%20%20%20ctx.ExecuteQuery()%3B%0A%20%20%20%20Log.Info(%22INFO%20---%20folder%20permission%20inheritance%20broken%20on%20the%20sharepoint%20client%20folder%22)%3B%0A%7D%3B%0A%0Actx.Load(clientFolder%2C%20f%20%3D%26gt%3B%20f.Name%2C%20f%20%3D%26gt%3B%20f.ListItemAllFields%2C%20f%20%3D%26gt%3B%20f.ListItemAllFields.HasUniqueRoleAssignments%2C%0A%20%20%20%20%20%20%20%20f%20%3D%26gt%3B%20f.ListItemAllFields.RoleAssignments.Include(r%20%3D%26gt%3B%20r.Member%2C%20r%20%3D%26gt%3B%20r.Member.LoginName%2C%20r%20%3D%26gt%3B%20r.Member.PrincipalType%2C%0A%20%20%20%20%20%20%20%20r%20%3D%26gt%3B%20r.RoleDefinitionBindings.Include(roleDef%20%3D%26gt%3B%20roleDef.Name%2C%20roleDef%20%3D%26gt%3B%20roleDef.Description)))%3B%0Actx.ExecuteQueryRetry()%3B%0A%0ARoleAssignmentCollection%20roleAssignments%20%3D%20clientFolder.ListItemAllFields.RoleAssignments%3B%0A%0Aforeach%20(RoleAssignment%20roleAssignment%20in%20roleAssignments.ToList())%0A%7B%0A%0A%20%20%20%20%20%20%20%20roleAssignment.DeleteObject()%3B%0A%20%20%20%20%20%20%20%20ctx.ExecuteQuery()%3B%0A%20%20%20%20%20%20%20%20Log.Info(%22INFO%20---%20permission%20removed%20for%3A%20%22%20%2B%20roleAssignment.Member.LoginName)%3B%0A%20%20%20%20%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%20worked%20but%20all%20permissions%20of%20the%20sub%20folders%20have%20also%20been%20removed.%3C%2FP%3E%3CP%3EHow%20can%20I%20remove%20all%20permissions%20of%20a%20folder%20without%20impacting%20the%20sub%20folders%20permissions.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EArioule%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1772937%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
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