Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get users by group in sharepoint

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..

like image 224
Adyt Avatar asked Nov 28 '08 03:11

Adyt


People also ask

How do I extract a user from a SharePoint group?

Here is the shortcut to export SharePoint users and groups permission to Excel: Open your SharePoint site in Internet Explorer. Navigate to either site permissions or any User group. Right-click on the users list page, choose the “Export to Excel” item.

How do I see users in SharePoint access?

Open your SharePoint site settings → Click “Site Permissions”. Click “Check Permissions” → Enter the username of the user whose permissions you want to check -> Click “Check Now”.


1 Answers

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

like image 98
Pedrin Avatar answered Oct 07 '22 14:10

Pedrin