共用方式為


SPRoleCollection class

NOTE: This API is now obsolete.

請改用新的SPRoleDefinitionSPRoleAssignment類別,來定義角色,並將使用者指派給它們。如需詳細資訊,請參閱Changes in the Authorization Object Model。(在Windows SharePoint Services 2.0, SPRoleCollection表示SPRole物件的集合和維護回溯相容性。)

Inheritance hierarchy

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

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'宣告
<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]或 Visual Basic,其中index是索引編號的集合中的角色,或者是網站群組的顯示名稱中的collRoles(index) 。

Examples

下列程式碼範例會將站台的所有使用者都加入指定的角色,為在 [網站] 下的所有子網站。

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();
        }
    }
}
注意事項注意事項

某些物件實作IDisposable介面,並且您必須避免之後不再需要保留這些物件在記憶體中。良好的程式碼撰寫方式的相關資訊,請參閱Disposing Objects

Thread safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

請參閱

參照

SPRoleCollection members

Microsoft.SharePoint namespace