在sharepoint中按组获取用户

时间:2022-10-24 20:47:00

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

任何人都可以告诉我如何使用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.

即时通讯使用c#,我试图通过使它成为一个控制台应用程序来做。

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

我是sharepoint的新手,我真的跳进了游泳池的深处,任何帮助都将受到高度赞赏。

cheers..

4 个解决方案

#1


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:

您需要知道的第一件事是,当您有一个包含用户/组字段的列表时,您必须知道它的类型。如果项目值中包含一个用户或组,则字段类型为SPFieldUserValue。但是,如果该字段具有多个用户/组选择,则字段类型为SPFieldUserValueCollection。我假设您的字段允许单个用户/组选择,并且您已经拥有以下对象:

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

现在,我们将检查用户/组的字段值并检索用户列表,与其类型无关(字段的名称为“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

Tks,PedroJoséBatista

#2


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.

注意:SPUser对象也可以是AD组(也就是说,“DOMAIN \ Domain Users”可能存在SPUser对象...这就是SPUser对象也包含属性IsDomainGroup的原因。

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

从这些信息中,您可以使用SPPrincipalInfo对象开始遍历AD组...但是它并不总是令人愉快。

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.

值得记住的一件事是SPGroup对象包含可以遍历AD组的ContainsCurrentUser属性...但是假设您有一个SPGroup对象可供使用。

Enjoy.
-Scott

#3


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;
    }

#4


0  

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

最好使用web.SiteGroups而不是web.Groups作为一个组可能会被该站点继承。

#1


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:

您需要知道的第一件事是,当您有一个包含用户/组字段的列表时,您必须知道它的类型。如果项目值中包含一个用户或组,则字段类型为SPFieldUserValue。但是,如果该字段具有多个用户/组选择,则字段类型为SPFieldUserValueCollection。我假设您的字段允许单个用户/组选择,并且您已经拥有以下对象:

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

现在,我们将检查用户/组的字段值并检索用户列表,与其类型无关(字段的名称为“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

Tks,PedroJoséBatista

#2


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.

注意:SPUser对象也可以是AD组(也就是说,“DOMAIN \ Domain Users”可能存在SPUser对象...这就是SPUser对象也包含属性IsDomainGroup的原因。

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

从这些信息中,您可以使用SPPrincipalInfo对象开始遍历AD组...但是它并不总是令人愉快。

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.

值得记住的一件事是SPGroup对象包含可以遍历AD组的ContainsCurrentUser属性...但是假设您有一个SPGroup对象可供使用。

Enjoy.
-Scott

#3


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;
    }

#4


0  

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

最好使用web.SiteGroups而不是web.Groups作为一个组可能会被该站点继承。