get users by group in sharepoint
Asked Answered
S

4

10

can anyone show me how to get the users within a certain group using sharepoint?

so i have a list that contains users and or groups. i want to retrieve all users in that list. is there a way to differentiate between whether the list item is a group or user. if its a group, i need to get all the users within that group.

im using c#, and im trying to do thins by making it a console application.

im new to sharepoint and im really jumping into the deep end of the pool here, any help would be highly appreciated.

cheers..

Sherburne answered 28/11, 2008 at 3:7 Comment(1)
Are you talking about SharePoint or Active Directory groups ?Pean
U
14

The first thing you need to know is that when you have a list with a User / Group field you must be aware of its type. When you have one user or group within the item value, the field type is SPFieldUserValue. However, if the field has multiple user / group selection the field type is SPFieldUserValueCollection.
I'll assume that your field allows a single user / group selection and you already has the following objects:

SPSite site;
SPWeb web;
SPListItem item;

Now, we'll check the field value for a user / group and retrieve a list of users, independant of which kind it is (the field's name is "Users").

SPFieldUserValue usersField = new SPFieldUserValue(mainWeb, item["Users"].ToString());
bool isUser = SPUtility.IsLoginValid(site, usersField.User.LoginName);
List<SPUser> users = new List<SPUser>();

if (isUser)
{
    // add a single user to the list
    users.Add(usersField.User);
}
else
{
    SPGroup group = web.Groups.GetByID(usersField.LookupId);

    foreach (SPUser user in group.Users)
    {
        // add all the group users to the list
        users.Add(user.User);
    }
}

I hope it helps you.

Tks,
Pedro José Batista

Unavoidable answered 28/11, 2008 at 16:56 Comment(3)
As Sergey Turin says below it is better to use SiteGroups because web.Groups only returns those groups with rights to the web referenced.Mcginley
you seem to have an error on the line "users.Add(user.User);". As "user" is already an SPUser object you can direclty add it to the List: users.Add(user);Parricide
Where do I run this stuff? Do I just put this in a .js file and run it from my console or something?Reproduction
R
5

note: an SPUser object can also be an AD Group (that is to say, an SPUser object might exist for "DOMAIN\Domain Users"... which is why the SPUser object also contains the property IsDomainGroup.

From this information you can start to traverse through AD groups using the SPPrincipalInfo objects... however it's not always pleasant.

One thing worth keeping in mind is that the SPGroup object includes the ContainsCurrentUser property which can traverse AD groups... this assumes you've got an SPGroup object to work from, however.

Enjoy.
-Scott

Radar answered 5/11, 2009 at 13:59 Comment(0)
H
0

This is better to use web.SiteGroups instead of web.Groups as a group might be inherited by that site.

Hereditary answered 11/3, 2011 at 14:10 Comment(0)
U
0
private bool IsMember()
    {
        bool isMember;
        SPSite site = new SPSite(SiteURL);
        SPWeb web = site.OpenWeb();
        isMember = web.IsCurrentUserMemberOfGroup(web.Groups["GroupName"].ID);
        web.Close();
        site.Close();
        return isMember;
    }
Unhappy answered 2/8, 2011 at 8:28 Comment(1)
That's not exactly what the OP asked. He wanted to list all members of a group no to check if account Xy is in a specific group.Parricide

© 2022 - 2024 — McMap. All rights reserved.