This documentation is archived and is not being maintained.

SPRoleCollection Class

Windows SharePoint Services 3

NOTE: This API is now obsolete.

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

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

[ObsoleteAttribute("Use the SPRoleDefinitionCollection class instead")]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public class SPRoleCollection : SPBaseCollection

Use the Roles property of the SPUser, SPGroup, or SPWeb class to return an SPRoleCollection object that represents the collection of roles for a user, group, or Web site. Use an indexer to return a single role from the collection. For example, if the collection is assigned to a variable named collRoles, use collRoles[index] in C#, or collRoles(index) in Visual Basic, where index is either the index number of the role in the collection or the display name of the site group.

The following code example adds all the users of a site to a specified role for all the subsites under the site.

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"];

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 Best Practices: Using Disposable Windows SharePoint Services Objects.

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