按组在sharepoint中获取用户 [英] get users by group in sharepoint

查看:248
本文介绍了按组在sharepoint中获取用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我如何使用sharepoint获取某个组中的用户?



所以我有一个包含用户和/或组的列表。我想检索该列表中的所有用户。是否有一种方法来区分列表项是否是组或用户。如果它的一个组,我需要得到该组中的所有用户。



im使用c#,并试图做一个控制台应用程序。







p> cheers ..

解决方案

你需要知道的第一件事是,当你有一个用户/你必须知道它的类型。当项目值中有一个用户或组时,字段类型为SPFieldUserValue。但是,如果字段具有多个用户/组选择,则字段类型为SPFieldUserValueCollection。

我假设您的字段允许单个用户/组选择,并且您已经具有以下对象:

  SPSite网站; 
SPWeb web;
SPListItem item;

现在,我们将检查用户/组的字段值并检索用户列表,

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

if(isUser)
{
//将单个用户添加到列表
users.Add(usersField.User);
}
else
{
SPGroup group = web.Groups.GetByID(usersField.LookupId);

foreach(group.Users中的SPUser用户)
{
//将所有组用户添加到列表
users.Add(user.User);
}
}

我希望它能帮助你。



Tks,

PedroJoséBatista


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

解决方案

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

这篇关于按组在sharepoint中获取用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆