Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically add user permission to a list in Sharepoint

How do I programmatically add user permissions to a list in Sharepoint? I want to add the permission "Contribute" to a user or group for a certain list. I'm using C#.

like image 329
Adyt Avatar asked Dec 11 '08 07:12

Adyt


1 Answers

You can do this using the SPRoleAssignment object, e.g.

// Assuming you already have SPWeb and SPList objects
...
SPRoleAssignment roleAssignment = new SPRoleAssignment("dom\\user", "user@dom", "user", "some notes");
SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
if (!myList.HasUniqueRoleAssignments)
{
    myList.BreakRoleInheritance(true); // Ensure we don't inherit permissions from parent
} 
myList.RoleAssignments.Add(roleAssignment);
myList.Update();
like image 94
Paul Nearney Avatar answered Oct 20 '22 15:10

Paul Nearney