Share via


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 Changes in the 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)

Syntax

<ObsoleteAttribute("Use the SPRoleDefinition class instead")> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
Public Class SPRole _
    Inherits SPMember

Dim instance As SPRole
[ObsoleteAttribute("Use the SPRoleDefinition class instead")]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
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.

For information about the default roles that are available in Windows SharePoint Services, 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 Best Practices: Using Disposable Windows SharePoint Services 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.

See Also

Reference

SPRole Members

Microsoft.SharePoint Namespace