10

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#.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Adyt
  • 1,442
  • 7
  • 26
  • 38

1 Answers1

11

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();
Paul Nearney
  • 6,965
  • 2
  • 30
  • 37
  • I think your comment "Ensure we don't inherit permissions from parent" is not consistent with the code, it should be myList,BreakRoleInheritance(false) for that. – csgero Dec 11 '08 at 08:44
  • @csgero - not according to MSDN - http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.breakroleinheritance.aspx – Paul Nearney Dec 11 '08 at 08:49