3

I need to know how to detect the limited access role, when looping through role definitions for a list item.

Item in code below is a listitem, using object model (c#).

Code so far is:

SPRoleAssignmentCollection roles = item.RoleAssignments;
foreach (SPRoleAssignment role in roles)
    {
        SPRoleDefinitionBindingCollection rbc = role.RoleDefinitionBindings;
       foreach (SPRoleDefinition rdef in rbc)
        {
          RoleAssignment.RoleDefinitionBindings.Add(rdef);
        Log("Adding role definition : Name(" + rdef.Name + ") Type(" + rdef.Type + ")");
        }
    }

problem is the RoleDefinition.Type enum does not have a limited property, so how can I check if the permission is limited / restricted access?

2 Answers 2

2

If you want to check if it has limited access, you can use this code:

SPRoleDefinition limitedAccessRole = SPContext.Current.Web.RoleDefinitions["Limited Access"];
foreach (SPRoleAssignment roleAssignment in item.RoleAssignments)
{
    if (roleAssignment.RoleDefinitionBindings.Contains(limitedAccessRole))
    {
        // Your limited access
    }
}  
3
  • Its a nice approach, I wonder if you could substitute in if rdef == limitedAccessRole, then its a sure winner. Another thing is wonder how it works on localized (non english sites).
    – user879
    Commented Dec 3, 2010 at 23:08
  • Yes you can change if, but you will need to itarete over roleAssigment.RoleDefinitionBindings to make the check. I didn't had any expirience in localization of permission, so I don't know. Can you post some information how to solve localization issue? Commented Dec 6, 2010 at 9:29
  • Please have a look at victors answer. I've edited it (i hope it will be accepted) with some localization details. It is not obvious that Guest will be resolved as Limited Access in english webs, but that is exactly the type. Commented Mar 1, 2013 at 12:14
2

To get a role definition without any localization issues and without hard coding use the SPRoleType enum:

Example:

if (roleAssignment.RoleDefinitionBindings.Contains(SPContext.Current.Web.RoleDefinitions.GetByType(SPRoleType.Guest)))
{
    // Do something
}

In German the name of the role is "Beschränkter Zugriff", but in English this is called "Limited access". Hardcoding the name of the role would not work in a multi-language environment.

1
  • Alternative that gets the "limited access" role. web.RoleDefinitions.Cast<SPRoleDefinition>().FirstOrDefault(role => role.Type == SPRoleType.Guest)
    – Thomas
    Commented Nov 10, 2016 at 10:25

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.