SPRole Class
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 Authorization Object Model. (In Windows SharePoint Services 2.0, SPRole represented a site group and is maintained for backward compatibility.)
Inheritance Hierarchy
System.Object
Microsoft.SharePoint.SPMember
Microsoft.SharePoint.SPRole
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
Syntax
'Declaration
<ObsoleteAttribute("Use the SPRoleDefinition class instead")> _
Public Class SPRole _
Inherits SPMember
'Usage
Dim instance As SPRole
[ObsoleteAttribute("Use the SPRoleDefinition class instead")]
public class SPRole : SPMember
Remarks
Use the Roles property of the SPUser, SPGroup, or SPWeb class to return an SPRoleCollection object that represents the collection of roles or role definitions 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 role.
Every role or role definition has a unique member ID (ID property), has the permissions associated with that membership, and can be represented by an SPMember object.
The following example assigns a role to an SPMember object.
Dim myMember As SPMember = site.Roles("Role_Name")
SPMember oMember = oWebsite.Roles["Role_Name"];
To perform any administrative tasks that affect settings for all Web applications in the server farm, a user must be a member of the SharePoint Administrator group.
The Administrator and Guest roles cannot be modified or deleted.
For general information about roles and security, see Security, Users, and Groups in Windows SharePoint Services.
For information about the default roles that are available in SharePoint Foundation, see the SPRoleType enumeration.
Examples
The following code example removes a specified user from a role in all the subsites under a site.
Dim site As SPWeb =
SPContext.Current.Site.AllWebs("Site_Name")
Dim subSites As SPWebCollection = site.Webs
Dim user As SPUser = site.Users("User_Name")
Dim subSite As SPWeb
For Each subSite In subSites
Dim role As SPRole = subSite.Roles("Role_Name")
role.RemoveUser(user)
Next subSite
using(SPWeb oParentWebsite = SPContext.Current.Site.AllWebs["Site_Name"])
{
SPWebCollection collWebsites = oParentWebsite.Webs;
SPUser oUser = oParentWebsite.Users["User_Name"];
foreach (SPWeb oWebsite in collWebsites)
{
SPRole oRole = oWebsite.Roles["Role_Name"];
oRole.RemoveUser(oUser);
oWebsite.Dispose();
}
}
Note
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.
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.