C# – Sharepoint – Retrieving user group and permission rights programmatically

csharepoint

currently I'm trying to retrieve all the groups that is in my sharepoint site. After which, I need to know which users are in the group and the level of site permission for each user. I'm using WSS 3.0 , developing in C# (visual studio 2008).
Help really needed as I'm still new in this area. Thanks in advance!

Best Answer

Groups can be found like:

SPSite siteCollection = new SPSite("site url");
SPWeb site = siteCollection.OpenWeb();

foreach(SPGroup group in site.Groups){
  Console.WriteLine(group.Name);

   foreach(SPUser u in group.Users){
         //will give you users in group, you can then grab the roles of the user
   }
}

To find what permissions a role has:

SPSite oSiteCollection = SPContext.Current.Site;
using(SPWeb oWebsite = oSiteCollection.AllWebs["Site_Name"])
{
    SPMember oMember = oWebsite.Roles["Role_Name"];
    oWebsite.Permissions[oMember].PermissionMask = 
        SPRights.ManageLists | SPRights.ManageListPermissions;
}

The permissions matrix can be found here

Related Topic