0

I am working on a Remote Event Receiver, and i want to do the following:-

  1. Break item permission + copy the permission.
  2. For all the SharePoint groups assigned to a list item >> grant them Read permission level.

here is my code:-

foreach (ListItem i in collListItem)
                {
i.BreakRoleInheritance(true,false);
                            foreach (RoleAssignment r  in i.RoleAssignments)
                            { 

but i am not sure how i can get all the security group associated with a list item + grant them Read permission? so i will make the item as Read-Only to all users?

1

Even after breaking permission inheritance, you can't directly change a user's permissions to Read -- you technically have to remove their existing permissions, then Add the Read permission.

First, even though you only want to grant a single permission (i.e. Read), you still need to construct a RoleDefinitionBindingCollection to hold that one RoleDefinition, because the RoleAssignmentCollection.Add only accepts a collection.

Then you have to loop through the existing RoleAssignments on the item to get a collection of users/groups (Principals) who already are assigned to the item. If you are certain that all items already inherit from the parent, you could just do this once and get the collection of Principals before you being the loop, which would be more efficient, but could miss any users already assigned through broken inheritance.

Then, when you call BreakRoleInheritance, use the false parameter that wipes out the permissions on the item instead of copying the existing. Then, finally you can loop through the collection of Principals you already have and create Reader Role Assignments for each Principal on the item.

RoleDefinition readerDef = ctx.Web.RoleDefinitions.GetByType(RoleType.Reader);
RoleDefinitionBindingCollection readOnlyBinding = new RoleDefinitionBindingCollection(clientContext);
readOnlyBinding.Add(readerDef);

foreach (ListItem i in collListItem)
{
    List<Principal> usersToGrantRead = new List<Principal>();

    foreach (RoleAssignment r  in i.RoleAssignments)
    {
        usersToGrantRead.Add(r.Member);
    }

    i.BreakRoleInheritance(false,false); // false clears out all permissions on the item
    ctx.ExecuteQuery();

    foreach (Principal p in usersToGrantRead)
    {
        i.RoleAssignments.Add(p, readOnlyBinding);
    }
    ctx.ExecuteQuery();
}
| improve this answer | |
  • i tried your code, but i get this error Cannot add a role assignment with empty role definition binding collection on the last line ctx.ExecuteQuery(); – SharePoint TestDev Feb 18 at 9:49
  • Did you get the first three lines where the RoleDefinitionBinding is built before the loop? – willman Feb 18 at 10:21
  • yes of course ... – SharePoint TestDev Feb 18 at 10:22
  • Actually, I thing the RoleAssignments actions take place instantly, try removing both ExecuteQuery references. – willman Feb 18 at 10:34

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.