如何:使用用户和组

上次修改时间: 2011年4月29日

适用范围: SharePoint Foundation 2010

本文内容
检索 SharePoint 组中的所有用户
检索特定的用户属性
检索网站集的所有组中的全部用户
将用户添加到 SharePoint 组

在 SharePoint Online 中提供

适用于 Microsoft.SharePoint.Client 命名空间 (JavaScript: SP) 中网站、列表和列表项的数据检索逻辑同样适用于用户和组。与该命名空间中的其他对象一样,您可以使用 CreationInformation 对象创建用户或组对象。

检索 SharePoint 组中的所有用户

Web 类 (JavaScript: Web) 的 SiteGroups 属性 (JavaScript: siteGroups) 可获取网站集中所有网站中的所有组。如以下示例所示,您可以使用 GetById(Int32) 方法 (JavaScript: getById(id)) 从组集合中返回特定组。Group 类 (JavaScript: Group) 的 Users 属性 (JavaScript: users) 可获取组中的所有用户。由于以下示例加载指定组中的用户,因此每个用户对象的所有属性均可用。

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class RetrieveAllUsersInGroup
    {
        static void Main()
        {
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
            GroupCollection collGroup = clientContext.Web.SiteGroups;
            Group oGroup = collGroup.GetById(7);
            UserCollection collUser = oGroup.Users;

            clientContext.Load(collUser);

            clientContext.ExecuteQuery();

            foreach (User oUser in collUser)
            {
                Console.WriteLine("User: {0}  ID: {1} Email: {2} Login Name: {3}", 
                    oUser.Title, oUser.Id, oUser.Email, oUser.LoginName);
            }
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class RetrieveAllUsersInGroup

        Shared Sub Main()
            Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
            Dim collGroup As GroupCollection = clientContext.Web.SiteGroups
            Dim oGroup As Group = collGroup.GetById(7)
            Dim collUser As UserCollection = oGroup.Users

            clientContext.Load(collUser)

            clientContext.ExecuteQuery()

            Dim oUser As User
            For Each oUser In collUser
                Console.WriteLine("User: {0}  ID: {1} Email: {2} Login Name: {3}", _
                                  oUser.Title, oUser.Id, oUser.Email, oUser.LoginName)
            Next oUser

        End Sub        
    End Class
End Namespace
var siteUrl = '/sites/MySiteCollection ';

function retrieveAllUsersInGroup() {

    var clientContext = new SP.ClientContext(siteUrl);
    var collGroup = clientContext.get_web().get_siteGroups();
    var oGroup = collGroup.getById(7);
    this.collUser = oGroup.get_users();
    clientContext.load(collUser);


    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    var userInfo = '';

    var userEnumerator = collUser.getEnumerator();
    while (userEnumerator.moveNext()) {
        var oUser = userEnumerator.get_current();
        this.userInfo += '\nUser: ' + oUser.get_title() + 
            '\nID: ' + oUser.get_id() + 
            '\nEmail: ' + oUser.get_email() + 
            '\nLogin Name: ' + oUser.get_loginName();
    }
      
    alert(userInfo);
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

检索特定的用户属性

以下示例对上一示例进行了修改,以便只返回特定组中每个用户的职位和电子邮件地址。该示例使用 Include<TSource>(IQueryable<TSource>, []) 方法(JavaScript: Include 语句)指定只有 Title (JavaScript: title) 和 Email (JavaScript: email) 属性可用于每个用户对象,并且调用其他属性将返回 PropertyOrFieldNotInitializedException

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class RetrieveSpecificUserProperties
    {
        static void Main()
        {
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
            GroupCollection collGroup = clientContext.Web.SiteGroups;
            Group oGroup = collGroup.GetById(7);
            UserCollection collUser = oGroup.Users;

            clientContext.Load(collUser,
                users => users.Include(
                    user => user.Title,
                    user => user.LoginName,
                    user => user.Email));

            clientContext.ExecuteQuery();

            foreach (User oUser in collUser)
            {
                Console.WriteLine("User: {0} Login name: {1} Email: {2}", 
                    oUser.Title, oUser.LoginName, oUser.Email);
            }
        }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class RetrieveSpecificUserProperties

        Shared Sub Main()
            Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
            Dim collGroup As GroupCollection = clientContext.Web.SiteGroups
            Dim oGroup As Group = collGroup.GetById(7)
            Dim collUser As UserCollection = oGroup.Users

            clientContext.Load(collUser, Function(users) users.Include( _
                                             Function(user) user.Title, _
                                             Function(user) user.LoginName, _
                                             Function(user) user.Email))

            clientContext.ExecuteQuery()

            Dim oUser As User
            For Each oUser In collUser
                Console.WriteLine("User: {0} Login name: {1} Email: {2}", _
                                  oUser.Title, oUser.LoginName, oUser.Email)
            Next oUser

        End Sub        
    End Class
End Namespace
var siteUrl = '/sites/MySiteCollection ';

function retrieveSpecificUserProperties() {

    var clientContext = new SP.ClientContext(siteUrl);
    var collGroup = clientContext.get_web().get_siteGroups();
    var oGroup = collGroup.getById(7);
    this.collUser = oGroup.get_users();
    clientContext.load(collUser, 'Include(Title, LoginName, Email)');


    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    var userInfo = '';

    var userEnumerator = collUser.getEnumerator();
    while (userEnumerator.moveNext()) {
        var oUser = userEnumerator.get_current();
        this.userInfo += '\nUser: ' + oUser.get_title() + 
            '\nEmail: ' + oUser.get_email() + 
            '\vLogin Name: ' + oUser.get_loginName();
    }
        
    alert(userInfo);
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

检索网站集的所有组中的全部用户

上面的示例演示如何返回指定组中的所有用户。若要返回网站集内所有组中的全部对象,您可以使用两次 Load<T>(T, []) 方法 (JavaScript: load(clientObject)) 来同时加载组集合和每个组中的用户集合,以便使每个用户和组的所有属性均可用。可使用 LINQ 查询方法语法包括每个组的每个用户集合,如以下示例所示。

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class RetrieveAllUsersAllGroups
    {
        static void Main()
        {
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
            GroupCollection collGroup = clientContext.Web.SiteGroups;

            clientContext.Load(collGroup);

            clientContext.Load(collGroup,
                groups => groups.Include(
                    group => group.Users));

            clientContext.ExecuteQuery();

            foreach (Group oGroup in collGroup)
            {
                UserCollection collUser = oGroup.Users;

                foreach (User oUser in collUser)
                {
                    Console.WriteLine("Group ID: {0} Group Title: {1} User: {2} Login Name: {3}", 
                        oGroup.Id, oGroup.Title, oUser.Title, oUser.LoginName);
                }
            }  
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class RetrieveAllUsersAllGroups

        Shared Sub Main()
            Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
            Dim collGroup As GroupCollection = clientContext.Web.SiteGroups

            clientContext.Load(collGroup)

            clientContext.Load(collGroup, Function(groups) groups.Include( _
                                              Function(group) group.Users))

            clientContext.ExecuteQuery()

            Dim oGroup As Group
            For Each oGroup In collGroup
                Dim collUser As UserCollection = oGroup.Users

                Dim oUser As User
                For Each oUser In collUser
                    Console.WriteLine("Group ID: {0} Group Title: {1} User: {2} Login Name: {3}", _
                                      oGroup.Id, oGroup.Title, oUser.Title, oUser.LoginName)
                Next oUser
            Next oGroup

        End Sub        
    End Class
End Namespace
var siteUrl = '/sites/MySiteCollection ';

function retrieveAllUsersAllGroups() {

    var clientContext = new SP.ClientContext(siteUrl);
    this.collGroup = clientContext.get_web().get_siteGroups();
    clientContext.load(collGroup);
    clientContext.load(collGroup, 'Include(Users)');

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    var userInfo = '';

    var groupEnumerator = collGroup.getEnumerator();
    while (groupEnumerator.moveNext()) {
        var oGroup = groupEnumerator.get_current();
        var collUser = oGroup.get_users();
        var userEnumerator = collUser.getEnumerator();
        while (userEnumerator.moveNext()) {
            var oUser = userEnumerator.get_current();
            this.userInfo += '\nGroup ID: ' + oGroup.get_id() + 
                '\nGroup Title: ' + oGroup.get_title() + 
                '\nUser: ' + oUser.get_title() + 
                '\nLogin Name: ' + oUser.get_loginName();
        }
    }
        
    alert(userInfo);
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

为了提高效率,您可以调用一次 Load<T>(T, []) 方法 (JavaScript: load(clientObject)) 并修改查询表达式,使其只包括特定的属性,如以下示例所示。

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class RetrieveAllUsersAllGroupsSpecificProperties
    {
        static void Main()
        {
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
            GroupCollection collGroup = clientContext.Web.SiteGroups;

            clientContext.Load(collGroup,
                groups => groups.Include(
                    group => group.Title,
                    group => group.Id,
                    group => group.Users.Include(
                        user => user.Title,
                        user => user.LoginName)));

            clientContext.ExecuteQuery();

            foreach (Group oGroup in collGroup)
            {
                UserCollection collUser = oGroup.Users;

                foreach (User oUser in oGroup.Users)
                {
                    Console.WriteLine("Group: {0} Group ID: {1} User: {2} Login Name: {3}", 
                        oGroup.Title, oGroup.Id, oUser.Title, oUser.LoginName);
                }
            }
         }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class RetrieveAllUsersAllGroupsSpecificProperties

        Shared Sub Main()
            Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
            Dim collGroup As GroupCollection = clientContext.Web.SiteGroups

            clientContext.Load(collGroup, Function(groups) groups.Include( _
                                         Function(group) group.Title, _
                                         Function(group) group.Id, _
                                         Function(group) group.Users.Include( _
                                             Function(user) user.Title, _
                                             Function(user) user.LoginName)))

            clientContext.ExecuteQuery()

            Dim oGroup As Group
            For Each oGroup In collGroup
                Dim collUser As UserCollection = oGroup.Users

                Dim oUser As User
                For Each oUser In oGroup.Users
                    Console.WriteLine("Group: {0} Group ID: {1} User: {2} Login Name: {3}", _
                                      oGroup.Title, oGroup.Id, oUser.Title, oUser.LoginName)
                Next oUser
            Next oGroup

        End Sub        
    End Class
End Namespace
var siteUrl = '/sites/MySiteCollection ';

function retrieveAllUsersAllGroupsSpecificProperties() {

    var clientContext = new SP.ClientContext(siteUrl);
    this.collGroup = clientContext.get_web().get_siteGroups();
    clientContext.load(collGroup, 'Include(Title,Id,Users.Include(Title,LoginName))');

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    var userInfo = '';

    var groupEnumerator = collGroup.getEnumerator();
    while (groupEnumerator.moveNext()) {
        var oGroup = groupEnumerator.get_current();
        var collUser = oGroup.get_users();
        var userEnumerator = collUser.getEnumerator();
        while (userEnumerator.moveNext()) {
            var oUser = userEnumerator.get_current();
            this.userInfo += '\nGroup ID: ' + oGroup.get_id() + 
                '\nGroup Title: ' + oGroup.get_title() + 
                '\nUser: ' + oUser.get_title() + 
                '\nLogin Name: ' + oUser.get_loginName();
        }
    }
        
    alert(userInfo);
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

将用户添加到 SharePoint 组

如果网站集中已包含要添加到某个组中的用户对象,则可以使用 AddUser(User) 方法 (JavaScript: addUser(user)) 将用户添加到该组的用户集合中。但是,如果该用户不存在,则应该使用 UserCreationInformation 类 (JavaScript: UserCreationInformation) 定义一个新用户,然后通过 Add(UserCreationInformation) 方法 (JavaScript: add(parameters)) 将该新用户添加到组的用户集合中,如以下代码示例所示。该示例使用 GetById(Int32) 方法 (JavaScript: getById(id)) 从网站集内的组集合返回特定组,然后定义一个新用户对象,并将其添加到组的用户集合中。

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class AddUserToSharePointGroup
    {
        static void Main()
        {   
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection ");
            GroupCollection collGroup = clientContext.Web.SiteGroups;
            Group oGroup = collGroup.GetById(6);

            UserCreationInformation userCreationInfo = new UserCreationInformation();
            userCreationInfo.Email = "alias@somewhere.com";
            userCreationInfo.LoginName = @"DOMAIN\alias";
            userCreationInfo.Title = "John";

            User oUser = oGroup.Users.Add(userCreationInfo);

            clientContext.ExecuteQuery(); 
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class AddUserToSharePointGroup

        Shared Sub Main()
            Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
            Dim collGroup As GroupCollection = clientContext.Web.SiteGroups
            Dim oGroup As Group = collGroup.GetById(7)

            Dim userCreationInfo As New UserCreationInformation()
            userCreationInfo.Email = "alias@somewhere.com"
            userCreationInfo.LoginName = "DOMAIN\alias"
            userCreationInfo.Title = "John"
            Dim oUser As User = oGroup.Users.Add(userCreationInfo)

            clientContext.ExecuteQuery()

        End Sub        
    End Class
End Namespace
var siteUrl = '/sites/MySiteCollection ';

function addUserToSharePointGroup() {

    var clientContext = new SP.ClientContext(siteUrl);
    var collGroup = clientContext.get_web().get_siteGroups();
    var oGroup = collGroup.getById(7);
    var userCreationInfo = new SP.UserCreationInformation();
    userCreationInfo.set_email('alias@somewhere.com');
    userCreationInfo.set_loginName('DOMAIN\alias');
    userCreationInfo.set_title('John');
    this.oUser = oGroup.get_users().add(userCreationInfo);
    
    clientContext.load(oUser);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

}

function onQuerySucceeded() {
        
    alert(this.oUser.get_title() + " added.");
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

有关在 Microsoft SharePoint Foundation 2010 Silverlight 对象模型上下文中使用客户端对象的信息和示例,请参阅使用 Silverlight 对象模型

请参阅

概念

如何:使用角色

如何:打破角色分配继承关系

授权、用户和组

数据检索概述

创建客户端对象

客户端对象模型准则

常见编程任务

其他资源

客户端类库

ECMAScript 类库