1

I'm adding permissions for a user for a specific SPListItem:

SPRoleAssignment roleAssignment = new SPRoleAssignment(user);
roleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions["ASpecificRole"]);
listItem.RoleAssignments.Add(roleAssignment);
listItem.Update();

I'm giving it a name to distinguish on which basis does he have the access, because he can also be provided access to the same resource from other part of the system.

I want to be able then to remove the specific type of the assignment for this particular user but without touching his permissions gained from other entitlements (which can give him the same type of access e.g. "Read").

The problem is when I access listItem.RoleAssignments I can only index them by number and when using listItem.RoleAssignments.Remove I can only remove it by index or by SPUser object.

Is it possible to get the specific Role Assignment with the name I've given it when adding?

Or is there a better approach?

2

I've developed some code to perform this task. Hope it helps somebody.

foreach (SPRoleAssignment roleAssignment in listItem.RoleAssignments)
{
    if (roleAssignment.Member.LoginName != user.LoginName)
    {
        continue;
    }

    foreach (SPRoleDefinition roleDefinitionBinding in roleAssignment.RoleDefinitionBindings)
    {
        if (roleDefinitionBinding.Name == "ASpecificRole")
        {
            roleAssignment.RoleDefinitionBindings.Remove(roleDefinitionBinding);
            roleAssignment.Update();
        }
    }
}
  • 1
    If this answers the original question, please mark is as the answer. – moe May 20 '17 at 16:58

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.