如何:使用角色

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

适用范围: SharePoint Foundation 2010

本文内容
创建角色定义
为网站上的角色分配用户
创建一个 SharePoint 组,并将该组添加到角色中

在 SharePoint Online 中提供

通过客户端对象模型,可以在网站集内创建自定义角色定义,并可以为网站、列表和列表项的特定角色分配新的或现有的用户和组。与在服务器对象模型中使用 SPRoleDefinitionBindingCollection 类相似,将用户或组连同 RoleDefinitionBindingCollection 对象 (JavaScript: RoleDefinitionBindingCollection) 一起添加到针对特定对象的角色分配集合。

创建角色定义

若要在网站集的角色定义集合中创建角色定义,请使用 BasePermissions 类 (JavaScript: BasePermissions) 定义一个权限集,并使用 RoleDefinitionCreationInformation 类 (JavaScript: RoleDefinitionCreationInformation) 定义一个包含该权限集的角色,然后向角色定义集合中添加此新角色。下面的示例演示如何创建用于创建和管理通知的角色。Order 属性 (JavaScript: order) 指定此新角色在网站集的"权限级别"页上列出的自定义角色定义中排在第四位。

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class CreateRoleDefinition
    {
        static void Main()
        {
            ClientContext oClientContext = new ClientContext("http://MyServer/sites/MySiteCollection");

            Web oWebsite = clientContext.Web;

            BasePermissions permissions = new BasePermissions();
            permissions.Set(PermissionKind.CreateAlerts);
            permissions.Set(PermissionKind.ManageAlerts);

            RoleDefinitionCreationInformation roleCreationInfo = new RoleDefinitionCreationInformation();

            roleCreationInfo.BasePermissions = permissions;
            roleCreationInfo.Description = "A new role with create and manage alerts permission";
            roleCreationInfo.Name = "Create and Manage Alerts";
            roleCreationInfo.Order = 4;

            RoleDefinition oRoleDefinition = oWebsite.RoleDefinitions.Add(roleCreationInfo);

            clientContext.ExecuteQuery();

            Console.WriteLine("{0} role created.", oRoleDefinition.Name);
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples

    Class CreateRoleDefinition

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

            Dim permissions As New BasePermissions()
            permissions.Set(PermissionKind.CreateAlerts)
            permissions.Set(PermissionKind.ManageAlerts)

            Dim roleCreationInfo As New RoleDefinitionCreationInformation()

            roleCreationInfo.BasePermissions = permissions
            roleCreationInfo.Description = "A new role with create and manage alerts permission"
            roleCreationInfo.Name = "Create and Manage Alerts"
            roleCreationInfo.Order = 4

            Dim oRoleDefinition As RoleDefinition = oWebsite.RoleDefinitions.Add(roleCreationInfo)

            clientContext.ExecuteQuery()

            Console.WriteLine("{0} role created.", oRoleDefinition.Name)

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

function createRoleDefinition() {

    var clientContext = new SP.ClientContext(siteUrl);
    var oWebsite = clientContext.get_web();

    var permissions = new SP.BasePermissions();
    permissions.set(SP.PermissionKind.createAlerts);
    permissions.set(SP.PermissionKind.manageAlerts);

    var roleCreationInfo = new SP.RoleDefinitionCreationInformation();
    roleCreationInfo.set_basePermissions(permissions);
    roleCreationInfo.set_description('A new role with create and manage alerts permission');
    roleCreationInfo.set_name('Create and Manage AlertsT');
    roleCreationInfo.set_order(4);

    this.oRoleDefinition = oWebsite.get_roleDefinitions().add(roleCreationInfo);

    clientContext.load(oRoleDefinition);

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

function onQuerySucceeded() {

    var roleInfo = oRoleDefinition.get_name() + ' role created.';
    alert(roleInfo);
}

function onQueryFailed(sender, args) {

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

为网站上的角色分配用户

若要为网站上的特定角色分配用户,可以为网站上下文创建 RoleDefinitionBindingCollection 对象 (JavaScript: RoleDefinitionBindingCollection),并向绑定集合对象添加角色定义,然后使用 Add(Principal, RoleDefinitionBindingCollection) 方法 (JavaScript: add(principal, roleBindings)) 将用户连同绑定集合一起添加到网站的角色分配中。在下面的示例中,将为上一示例中定义的角色分配现有网站集用户。

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class AddUserToRole
    {
        static void Main()
        {
            ClientContext oClientContext = new ClientContext("http://MyServer/sites/MySiteCollection/MyWebSite");
            Web oWebsite = clientContext.Web;

            Principal oUser = oWebsite.SiteUsers.GetByLoginName(@"DOMAIN\alias");

            RoleDefinition oRoleDefinition = oWebsite.RoleDefinitions.GetByName("Create and Manage Alerts");
            RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(clientContext);
            collRoleDefinitionBinding.Add(oRoleDefinition);

            RoleAssignment oRoleAssignment = oWebsite.RoleAssignments.Add(oUser, collRoleDefinitionBinding);

            clientContext.Load(oUser,
                user => user.Title);

            clientContext.Load(oRoleDefinition,
                role => role.Name);

            clientContext.ExecuteQuery();

            Console.WriteLine("{0} added with {1} role.", oUser.Title, oRoleDefinition.Name);
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples

    Class AddUserToRole

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

            Dim oUser As Principal = oWebsite.SiteUsers.GetByLoginName("domain\alias")
            Dim oRoleDefinition As RoleDefinition = oWebsite.RoleDefinitions.GetByName("Create and Manage Alerts")
            Dim collRoleDefinitionBinding As New RoleDefinitionBindingCollection(clientContext)
            collRoleDefinitionBinding.Add(oRoleDefinition)

            Dim oRoleAssignment As RoleAssignment = oWebsite.RoleAssignments.Add(oUser, collRoleDefinitionBinding)

            clientContext.Load(oUser, _
                Function(user) user.Title)

            clientContext.Load(oRoleDefinition, _
                Function(role) role.Name)

            clientContext.ExecuteQuery()

            Console.WriteLine("{0} added with {1} role.", oUser.Title, oRoleDefinition.Name)

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

function addUserToRole() {

    var clientContext = new SP.ClientContext(siteUrl);
    var oWebsite = clientContext.get_web();

    this.oUser = oWebsite.get_siteUsers().getByLoginName('DOMAIN\\alias');
    this.oRoleDefinition = oWebsite.get_roleDefinitions().getByName('Create and Manage Alerts');

    var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);        

    collRoleDefinitionBinding.add(oRoleDefinition);

    var oRoleAssignment = oWebsite.get_roleAssignments().add(oUser, collRoleDefinitionBinding);

    clientContext.load(oUser, 'Title');
    clientContext.load(oRoleDefinition, 'Name');

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

function onQuerySucceeded() {

    var roleInfo = oUser.get_title() + ' assigned to ' + oRoleDefinition.get_name();
    alert(roleInfo);
}

function onQueryFailed(sender, args) {

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

创建一个 SharePoint 组,并将该组添加到角色中

与通过客户端对象模型创建大多数其他对象一样,使用 GroupCreationInformation 类 (JavaScript: GroupCreationInformation) 定义一个新组,然后使用 Add(GroupCreationInformation) 方法 (JavaScript: add(parameters)) 将该组添加到网站集中的网站组集合中。如同为角色分配用户一样,通过以下方式为网站上的特定角色分配一个组:为网站上下文创建 RoleDefinitionBindingCollection 对象 (JavaScript: RoleDefinitionBindingCollection),将角色定义添加到绑定集合对象中,然后使用 Add(Principal, RoleDefinitionBindingCollection) 方法 (JavaScript: add(principal, roleBindings)) 将该组连同绑定集合一起添加到网站的角色分配中。下面的示例将创建一个组,并将该组分配给网站上的 Contribute 角色。

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class CreateGroupAddToRole
    {
        static void Main()
        {
            ClientContext oClientContext = new ClientContext("http://MyServer/sites/MySiteCollection/MyWebSite");
            Web oWebsite = clientContext.Web;

            GroupCreationInformation groupCreationInfo = new GroupCreationInformation();
            groupCreationInfo.Title = "My New Group";
            groupCreationInfo.Description = "Description of new group.";
            Group oGroup = oWebsite.SiteGroups.Add(groupCreationInfo);

            RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(clientContext);

            RoleDefinition oRoleDefinition = oWebsite.RoleDefinitions.GetByType(RoleType.Contributor);

            collRoleDefinitionBinding.Add(oRoleDefinition);

            oWebsite.RoleAssignments.Add(oGroup, collRoleDefinitionBinding);

            clientContext.Load(oGroup,
                group => group.Title);

            clientContext.Load(oRoleDefinition,
                role => role.Name);

            clientContext.ExecuteQuery();

            Console.WriteLine("{0} created and assigned {1} role.", oGroup.Title, oRoleDefinition.Name);        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples

    Class CreateGroupAddToRole

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

            Dim groupCreationInfo As New GroupCreationInformation()
            groupCreationInfo.Title = "My New Group "
            groupCreationInfo.Description = "Description of new group."
            Dim oGroup As Group = oWebsite.SiteGroups.Add(groupCreationInfo)

            Dim collRoleDefinitionBinding As New RoleDefinitionBindingCollection(clientContext)

            Dim oRoleDefinition As RoleDefinition = oWebsite.RoleDefinitions.GetByType(RoleType.Contributor)

            collRoleDefinitionBinding.Add(oRoleDefinition)

            oWebsite.RoleAssignments.Add(oGroup, collRoleDefinitionBinding)

            clientContext.Load(oGroup, _
                 Function(group) group.Title)

            clientContext.Load(oRoleDefinition, _
                 Function(role) role.Name)

            clientContext.ExecuteQuery()

            Console.WriteLine("{0} created and assigned {1} role.", oGroup.Title, oRoleDefinition.Name)

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

function createGroupAddToRole() {

    var clientContext = new SP.ClientContext(siteUrl);
    this.oWebsite = clientContext.get_web();

    var groupCreationInfo = new SP.GroupCreationInformation();
    groupCreationInfo.set_title('My New GroupT');
    groupCreationInfo.set_description('Description of new group.');
    this.oGroup = oWebsite.get_siteGroups().add(groupCreationInfo);

    var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);

    this.oRoleDefinition = oWebsite.get_roleDefinitions().getByType(SP.RoleType.contributor);
    collRoleDefinitionBinding.add(oRoleDefinition);

    var collRollAssignment = oWebsite.get_roleAssignments();
    collRollAssignment.add(oGroup, collRoleDefinitionBinding);

    clientContext.load(oGroup, 'Title');
    clientContext.load(oRoleDefinition, 'Name');

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

function onQuerySucceeded() {

    var roleInfo = oGroup.get_title() + ' created and assigned to ' + oRoleDefinition.get_name();
    alert(roleInfo);
}

function onQueryFailed(sender, args) {

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

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

请参阅

概念

如何:使用用户和组

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

授权、用户和组

数据检索概述

创建客户端对象

客户端对象模型准则

常见编程任务

其他资源

客户端类库

ECMAScript 类库