Programmatically add user permission to a list in Sharepoint
Asked Answered
H

1

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

Haugen answered 11/12, 2008 at 7:51 Comment(0)
D
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();
Donela answered 11/12, 2008 at 8:23 Comment(2)
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.Ellsworthellwood
@Ellsworthellwood - not according to MSDN - msdn.microsoft.com/en-us/library/…Donela

© 2022 - 2024 — McMap. All rights reserved.