In this post, I would like to discuss how to report on or retrieve SharePoint User Groups, SharePoint Users and Domain Groups on a given site or a site collection. Although this information is available to SharePoint Administrators by clicking on ‘Site Actions’ and ‘Site Permissions’, there are occasions when programmatically this information might be required.
Using site As New SPSite("http") For Each _site As SPWeb In site.AllWebs Using Web As SPWeb = _site For Each ra As SPRoleAssignment In Web.RoleAssignments ' Site Groups If TypeOf ra.Member Is SPGroup Then ‘ Business logic ra.Member.Name ' Site Group Permissions For Each rd As SPRoleDefinition In ra.RoleDefinitionBindings ‘ Business Logic rd.Name Next End If ' Site Users / Domain Groups If TypeOf ra.Member Is SPUser Then ‘ Business Logic ra.Member.Name ' Site Users / Domain Groups Permissions For Each rd As SPRoleDefinition In ra.RoleDefinitionBindings ‘ Business Logic rd.Name Next End If Next End Using Next End Using
In the code above I am traversing through a site.allwebs to get the SharePoint Groups, Sharepoint Users and Domain Groups on the site. You can make this further interesting by finding out if site has unique permissions or not or if any list on site has unique permissions.
Also using the same code for a SPWeb instead of site.allWebs in a loop you can query the permissions to different groups on a single SharePoint site.