tags:

views:

1573

answers:

1

Hi all, 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!

A: 

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

Daniel Pollard
thanks for the reply but i hit error for listGroup += (groupCol.Name); 'Microsoft.SharePoint.SPGroupCollection' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'Microsoft.SharePoint.SPGroupCollection' could be found (are you missing a using directive or an assembly reference?)foreach(SPUser singleUser in group.Users)The name 'group' does not exist in the current context Pls advice, thanks!
It looks like your variable groupCol is of type SPGroupCollection, which doesn't have a Name property (the items in this collection however do). The code I provided doesn't do anything like that. You should post your code so we can see what you are doing wrong.
Daniel Pollard
Do you want me to paste it here? SPGroupCollection groupCol = oWebsite.Groups; foreach (SPGroup Group in oWebsite.Groups) { //ListGroup.Text += (Group.Name) + "<BR>"; Label1.Text += (Group.Name) + "<BR>"; }output is :Team Site MembersTeam Site OwnersTeam Site Visitorsany idea why? Thanks
Team Site MembersTeam Site OwnersTeam Site Visitorsis the outcome instead of the groups in my sharepoint site. Any idea why? Please guide. Thanks