Click to Rate and Give Feedback
Community Content
In this section
Statistics Annotations (0)
Collapse All/Expand All Collapse All
This page is specific to
The 2010 product release

Other versions are also available for the following:
SPRoleAssignment Class

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

System..::.Object
  Microsoft.SharePoint..::.SPRoleAssignment

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
Visual Basic (Declaration)
<SubsetCallableTypeAttribute> _
Public NotInheritable Class SPRoleAssignment
Visual Basic (Usage)
Dim instance As SPRoleAssignment
C#
[SubsetCallableTypeAttribute]
public sealed class SPRoleAssignment

To grant a user or group permission to securable content, 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 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.

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

    SPRoleAssignment oRoleAssignment = new 
SPRoleAssignment("DOMAIN\\User_Alias","User_Alias@Somewhere.com","Displ
ay_Name","Notes");

    SPRoleDefinitionBindingCollection collRoleDefinitionBindings = oRoleAssignment.RoleDefinitionBindings;

    collRoleDefinitionBindings.Add(collRoleDefinitions["Role_Definition_Name"]);

    collRoleAssignments.Add(oRoleAssignment);
}
Visual Basic
Using oWebsite As SPWeb = SPContext.Current.Site.AllWebs("Subsite_Name")
    Dim collRoleDefinitions As SPRoleDefinitionCollection = oWebsite.RoleDefinitions
    Dim collRoleAssignments As SPRoleAssignmentCollection = oWebsite.RoleAssignments

    Dim oRoleAssignment As New SPRoleAssignment("DOMAIN\User_Alias","User_Alias@Somewhere.com","Displ ay_Name","Notes")

    Dim collRoleDefinitionBindings As SPRoleDefinitionBindingCollection = oRoleAssignment.RoleDefinitionBindings

    collRoleDefinitionBindings.Add(collRoleDefinitions("Role_Definition_Name"))

    collRoleAssignments.Add(oRoleAssignment)
End Using
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 Disposing 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.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker