Export (0) Print
Expand All

SPRole 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, SPRole represented a site group and is maintained for backward compatibility.)

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

[ObsoleteAttribute("Use the SPRoleDefinition class instead")]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public class SPRole : SPMember

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.

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.

The following code example removes a specified user from a role in all the subsites under a site.

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();
    }
}
NoteNote

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.

Community Additions

ADD
Show:
© 2014 Microsoft