0

How can I change the list permission to be read only for all groups in the site? programmatically.

This is my code:

   list.BreakRoleInheritance(true);
        SPRoleDefinition roleDefinition = list.ParentWeb.RoleDefinitions.GetByType(SPRoleType.Reader);
        SPRoleAssignment roleAssignment;
        foreach (SPGroup group in list.ParentWeb.SiteGroups) {
            roleAssignment = new SPRoleAssignment(group);
            list.RoleAssignments.Add(new SPRoleAssignment(group));
            roleAssignment.RoleDefinitionBindings.RemoveAll();
            roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
            list.RoleAssignments.Add(roleAssignment);
        }
            list.Update();

but it's not working!, it's adding the read permission to the current permissions, but not removing the old permissions.

Thanks

5
  • I think you need to remove first, update the list, then add. Try that and LMK Sep 23, 2013 at 15:22
  • so I need to make 2 foreach? Sep 23, 2013 at 16:24
  • I think so, give it a try, after RemoveAll, call List.Update, then add and List.Update again. Sep 23, 2013 at 16:25
  • it did not work .. Sep 23, 2013 at 18:52
  • I was hoping to go from memory. I'm pulling up my code now. I'll answer shortly ;) Sep 23, 2013 at 18:57

2 Answers 2

0

Here's a snippet from my code, works great for me. There is no list.Update() after the final line where we add the assignment. I vaguely remember the ResetRoleInheritance was our saving grace with this.

   SPRoleDefinition read = web.RoleDefinitions["Read"];
   SPRoleAssignment memberAssignment = new SPRoleAssignment(principal);
   memberAssignment.RoleDefinitionBindings.Add(read);
   SPList list = web.Lists[listName];
   list.ResetRoleInheritance();
   list.BreakRoleInheritance(true);
   list.Update();
   list.RoleAssignments.Add(memberAssignment);
6
  • but don't you break the inheritance for the list? Sep 23, 2013 at 19:10
  • 3rd line from the bottom. ;) Sep 23, 2013 at 19:12
  • Hi David, thanks for your code, I did the same but with looping all over the site groups and giving them permission over the list, now I have each group with read permission along with its default permission, so I have something like: Approvers: Read,Approve Sep 23, 2013 at 19:19
  • here is my code: SPRoleDefinition read = list.ParentWeb.RoleDefinitions.GetByType(SPRoleType.Reader); list.ResetRoleInheritance(); list.BreakRoleInheritance(true); list.Update(); SPRoleAssignment roleAssignment; foreach (SPGroup group in list.ParentWeb.SiteGroups) { roleAssignment = new SPRoleAssignment(group); roleAssignment.RoleDefinitionBindings.Add(read); list.RoleAssignments.Add(roleAssignment); } list.Update(); Sep 23, 2013 at 19:21
  • it worked with brearoleinheritance(false) as Lgaud said, with ur code Sep 23, 2013 at 19:25
0

BreakRoleInheritance(true) breaks inheritance but copies the existing permissions; BreakRoleInheritance(false) will not copy the permissions over.

1
  • now am having an error on this code: roleAssignment = new SPRoleAssignment(group); list.RoleAssignments.Add(roleAssignment); that the group doesn't exist.. anyone can REALLY help me with this, know what's wrong with my code, or how to make all groups have read only on the list? Sep 23, 2013 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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