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

  • I think you need to remove first, update the list, then add. Try that and LMK – David Lozzi Sep 23 '13 at 15:22
  • so I need to make 2 foreach? – Jack Nodovan Sep 23 '13 at 16:24
  • I think so, give it a try, after RemoveAll, call List.Update, then add and List.Update again. – David Lozzi Sep 23 '13 at 16:25
  • it did not work .. – Jack Nodovan Sep 23 '13 at 18:52
  • I was hoping to go from memory. I'm pulling up my code now. I'll answer shortly ;) – David Lozzi Sep 23 '13 at 18:57
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);
  • but don't you break the inheritance for the list? – Jack Nodovan Sep 23 '13 at 19:10
  • 3rd line from the bottom. ;) – David Lozzi Sep 23 '13 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 – Jack Nodovan Sep 23 '13 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(); – Jack Nodovan Sep 23 '13 at 19:21
  • it worked with brearoleinheritance(false) as Lgaud said, with ur code – Jack Nodovan Sep 23 '13 at 19:25
0

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

  • 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? – Jack Nodovan Sep 23 '13 at 18:49

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.