Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
SPRoleAssignment Class
Collapse the table of content
Expand the table of content

SPRoleAssignment Class

Windows SharePoint Services 3

Defines the role assignments for a user or group on the current object.


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

[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public sealed class SPRoleAssignment

To add a user to a Web site, you create a role assignment object, set the user or group for the role assignment, add the appropriate role definitions, and add the object to the collection of role assignments for the list item, list, Web site, or securable object.

Use the RoleAssignments property of the ISecurableObject interface, or of the SPList, SPListItem, or SPWeb class, to return the collection of role assignments for the given object.

Use an indexer to return a single item from the collection. For example, if the collection is assigned to a variable named collRoleAssignments, use collRoleAssignments[index] in C#, or collRoleAssignments(index) in Visual Basic, where index is either the index number of the item in the collection or a string that contains the name of the role definition.

The following code example creates a new role assignment for a user, adds a new role definition to the bound role definitions for the role assignment, and then adds the new role assignment to the collection of role assignments for the Web site.

using(SPWeb oWebsite = SPContext.Current.Site.AllWebs["Subsite_Name"])
    SPRoleDefinitionCollection collRoleDefinitions = oWebsite.RoleDefinitions;
    SPRoleAssignmentCollection collRoleAssignments = oWebsite.RoleAssignments;

    SPRoleAssignment oRoleAssignment = new 

    SPRoleDefinitionBindingCollection collRoleDefinitionBindings = oRoleAssignment.RoleDefinitionBindings;



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

© 2015 Microsoft