Easy Way To Add Role Assignments

I have to do this all the time. If you have a similar requirement, just use the following method:

[csharp]

public static void AddRoleAssignment(ISecurableObject securable, SPPrincipal principal, SPRoleDefinition[] roleDefinitions)
{
var roleDefinitionBindings = new SPRoleDefinitionBindingCollection();
foreach (SPRoleDefinition definition in roleDefinitions)
{
roleDefinitionBindings.Add(definition);
}
var roleAssignment = new SPRoleAssignment(principal);
roleAssignment.ImportRoleDefinitionBindings(roleDefinitionBindings);
securable.RoleAssignments.Add(roleAssignment);
}

[/csharp]

The usage of it is pretty straightforward, you have an ISecurableObject parameter to expose role assignments for an object, an SPPrincipal to represent a user or group, and a SPRoleDefinition array to act as a container for role definitions.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *