How to allow Full Control permission to a user in a Sharepoint List

The SPWeb has a  RoleDefinitions property that return a list of SPRoleDefinition.

Each SPRoleDefinition is a permission level ; for instance in the next code snippe,t web.Roledefinitions will return a collection of 6 roles; they match the permission Levels of the web site :

image

If you want to assign a permission level to a user on a specific list, you need to create a new RoleAssignement (SPRoleAssignment class) that can be added to the SPList. The new permission must be linked to the role assignment by adding it to a collection called RoleDefinitionBindings.

 

SPWeb web =SPControl.GetContextWeb(HttpContext.Current); //  use RunWithElevatedPrivilege here
SPList myList = web.Lists["MyList"];
myList.BreakRoleInheritance(false);
SPRoleDefinition fullRoleDef = web.RoleDefinitions["Full Control"];
SPUser user = web.EnsureUser(“LITWARE\SERGE”);
SPRoleAssignment newRoleassignment= new SPRoleAssignment(user);
newRoleassignment.RoleDefinitionBindings.Add(fullRoleDef);
myList.RoleAssignments.Add(newRoleassignment);
myList.Update();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s