1

I'm using the Client Object Model and trying to remove a specific RoleDefinition from a RoleDefinitionBindingCollection in a RoleAssignment.

This is the code I'm using:

        Web web = context.Web;
        List list = context.Web.Lists.GetByTitle("List");
        context.Load(
            list,
            l => l.RoleAssignments.Include(
                ra => ra.Member,
                ra => ra.RoleDefinitionBindings
                )
            );

        context.ExecuteQuery();

        List<RoleDefinition> toDelete = new List<RoleDefinition>();

        foreach (RoleAssignment ra in list.RoleAssignments) {
            toDelete.Clear();

            foreach (RoleDefinition rd in ra.RoleDefinitionBindings) {
                if (rd.RoleTypeKind == RoleType.Administrator) {
                    toDelete.Add(rd);                        
                }
            }

            if (toDelete.Count > 0) {
                toDelete.ForEach(td => ra.RoleDefinitionBindings.Remove(td));
            }                               
        }

        context.ExecuteQuery();

I can see that the RoleDefinition is removed from the parent RoleDefinitionBindings list, but nothing happens. There is no error and the permissions on the list stays exactly as they were after the context.ExecuteQuery()

All the examples I found are showing how to remove an entire RoleAssignment, or how to delete the RoleDefinition from the site.

2 Answers 2

0

Try calling list.Update(); before context.ExecuteQuery();

1
  • Nope... still nothing. any ideas ?
    – Shlomi
    Commented Jun 9, 2015 at 10:22
0

Please check the following solution:

if (list.HasUniqueRoleAssignments)
{ 
     RoleAssignmentCollection oRoleAssignments = list.RoleAssignments;
     clientContext.Load(oRoleAssignments);
     clientContext.ExecuteQuery();

     foreach (RoleAssignment oRoleAssignment in oRoleAssignments)
     {
            clientContext.Load(oRoleAssignment, r => r.Member, r => r.RoleDefinitionBindings);
            clientContext.ExecuteQuery();
            Principal oPrincipal = oRoleAssignment.Member;
            if (oPrincipal.LoginName.Equals(userToRemove) || oPrincipal.Title.Equals(userToRemove))
            {
                  foreach (RoleDefinition rd in oRoleAssignment.RoleDefinitionBindings)
                  {
                       if (rd.RoleTypeKind == RoleType.Contributor)
                       {
                             clientContext.Load(rd); oRoleAssignment.RoleDefinitionBindings.Remove(rd);
                                    oRoleAssignment.Update();
                                    clientContext.Load(oRoleAssignment, r => r.Member, r => r.RoleDefinitionBindings);
                                    list.Update();
                                    clientContext.ExecuteQuery();
                                }
                            }
                        }
                    }
                }

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.