Question

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?

Was it helpful?

Solution

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();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top