views:

2975

answers:

2

how do i programatically add user permission to a list in sharepoint. i want to add the permission "Contribute" to a user or group for a certain list. im using c#

Thanks..

+4  A: 

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
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
@csgero - not according to MSDN - http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.breakroleinheritance.aspx
Paul Nearney
thanks.. your answer really helped alot
Adit
A: 

but what if we want to assign this permission to the group by the same way ?

Jaafar