Get the users and group details having permissions on a list/listitem using RoleAssignments

Posted: February 25, 2011 in Code

This post explains how you can fins the list of users who are having permissions to a particular list or a list item. Also in case if a group is directly assigned permissions we can loop through the group to find each users of that group too.

We can access the permission details of a List  or a ListItem  using the properties

  • List.RoleAssignments
  • ListItem.RoleAssignments

Below code explains how to get SPUser details form the RoleAssignment property.

    private void AddListRoleAssignmentNodes(SPList objList)
    {   
      if (objList.HasUniqueRoleAssignments)
      {
         SPRoleAssignmentCollection oRoleAssignments =
            objList.RoleAssignments;

         foreach (SPRoleAssignment oRoleAssignment in oRoleAssignments)
         {
            SPPrincipal oPrincipal = oRoleAssignment.Member;
            try
            {
               // Retrieve users having explicit permissions on the list
               SPUser oRoleUser = (SPUser)oPrincipal;
            }
            catch (Exception ex)
            {
               string msg = ex.Message;
            }
            try
            {
               // Retrieve user groups having permissions on the list
               SPGroup oRoleGroup = (SPGroup)oPrincipal;

               if (oRoleGroup.Users.Count > 0)
               { 
                  string strGroupName = oRoleGroup.Name;
                  // Add code here to retrieve Users inside this User-Group
               }
            }
            catch (Exception ex)
            {
               string msg = ex.Message;
            }
         }
      }
   }

The first try catch gets all the users and second one gets all the groups that are having permissions on the list/list item.

Advertisements
Comments
  1. Vinay says:

    Hi,

    I believe the above script retrieves user list at a List level or Document Library level. Within the document library, if there are folders and sub-folders assigned with explicit permissions, how do we retrieve those user list?.. What are the webservices available to retrieve the user list at a sub folder level within a document library? Please let me know.

    Appreciate your help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s