SPRoleCollection 类

请注意:此 API 现在已过时。

Use the new SPRoleDefinition and SPRoleAssignment classes instead, to define roles and to assign users to them. For more information, see Changes in the Authorization Object Model. (In Windows SharePoint Services 2.0, SPRoleCollection represented a collection of SPRole objects and is maintained for backward compatibility.)

继承层次结构

System.Object
  Microsoft.SharePoint.Administration.SPAutoSerializingObject
    Microsoft.SharePoint.SPBaseCollection
      Microsoft.SharePoint.SPRoleCollection

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
<ObsoleteAttribute("Use the SPRoleDefinitionCollection class instead")> _
Public Class SPRoleCollection _
    Inherits SPBaseCollection
用法
Dim instance As SPRoleCollection
[ObsoleteAttribute("Use the SPRoleDefinitionCollection class instead")]
public class SPRoleCollection : SPBaseCollection

备注

使用SPUserSPGroupSPWeb类的Roles属性可返回SPRoleCollection对象,表示角色的用户、 组或网站的集合。使用索引器从集合中返回单个角色。例如,如果集合分配给名为collRoles的变量中,使用 C# 中的collRoles[index]或collRoles(index)在 Visual Basic 中,其中index是集合中的角色的索引号,或者网站用户组的显示名称。

示例

下面的代码示例将添加到指定的角色的站点下的所有子网站的网站的所有用户。

Dim site As SPWeb = 
    SPContext.Current.Site.AllWebs("Site_Name")
Dim subSites As SPWebCollection = site.Webs
Dim users As SPUserCollection = site.Users
Dim user As SPUser

For Each user In  users

    Dim subSite As SPWeb

    For Each subSite In  subSites

        Dim role As SPRole = subSite.Roles("Role_Name")

        role.AddUser(user)

    Next subSite

Next user 
using(SPWeb oWebsite = SPContext.Current.Site.AllWebs["Site_Name"])
{
    SPWebCollection collWebsites = oWebsite.Webs;
    SPUserCollection collUsers = oWebsite.Users;

    foreach (SPUser oUser in collUsers)
    {
        foreach (SPWeb oWebsiteCurrent in collWebsites)
        {
            SPRole oRole = oWebsite.Roles["Role_Name"];
            oRole.AddUser(oUser);
            oWebsiteCurrent.Dispose();
        }
    }
}

备注

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

线程安全性

该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。

另请参阅

引用

SPRoleCollection 成员

Microsoft.SharePoint 命名空间