I seam to always get asked this question on every project. This will be a very basic post to show you how to assign permissions through the SharePoint API.
When working with permissions through the SharePoint API, there are some key objects to take note of.
- SPUser - A actual security object in SharePoint (AD User, AD Group or Forms Based User or Group).
- SPGroup - A SharePoint group defined through SharePoint.
- SPRoleDefinition - A actual role (permission level) defined in SharePoint (Read, Full Control etc...)
- SPRoleAssignment - The actual assignment between a SPUser/SPGroup and a SPRoleDefinition
Assign a User to a SharePoint Group
To assign a user to a SharePoint group we need to get the SPGroup object and add a user with the AddUser method. This method has 2 overloads, the first takes a SPUser object and the second takes some specific parameters (login name, email, name, notes). Below is the code in both formats.
// ******************** Adding a SPUser to a SharePoint Group ********************
// Create the site that contains our list
SPSite oSite = new SPSite("<<my site url>>");
// Open the web object
SPWeb oWeb = spSite.OpenWeb();
// Get the group that we want to add the user to
SPGroup oGroup = oWeb.Groups["<<group name>>"];
// Get the user that we want to add to the group
SPUser oUser = oWeb.AllUsers["domain\login"];
// Now we add the user to the groups user collection
oGroup.AddUser(oUser);
// Update the group
oGroup.Update();
// ******************** Adding a User to a SharePoint Group ********************
// Create the site that contains our list
SPSite oSite = new SPSite("<<my site url>>");
// Open the web object
SPWeb oWeb = spSite.OpenWeb();
// Get the group that we want to add the user to
SPGroup oGroup = oWeb.Groups["<<group name>>"];
// Now we add the user to the groups user collection
oGroup.AddUser("login", "email", "name", "notes");
// Update the group
oGroup.Update();
Assigning Roles (Permission Levels) to a User or SharePoint Group
To assign permission to a user (account) or a SharePoint group there are some objects that we need to look at in a certain order. The first thing we need to do is get the the security principal that we want to assign the role to (SPUser or SPGroup). The next thing we need to do it get the actual permission (role) that we want to assign (ex: Read, Full Control etc...). Then we need to create a SPRoleAssignment object and on the constructor pass it in the SPUser or SPGroup (security principal) that we want to assign the permissions to. Now we need to add the role definition to the RoleDefinitionBindings collection of the role assignment object. Then we need to add the actual role assignment to the web (site) and update the web. Below is the full code lisitng.
// Create the site that contains our list
SPSite oSite = new SPSite("<<my site url>>");
// Open the web object
SPWeb oWeb = oSite.OpenWeb();
// Get the group that we want to add the user to
SPGroup oGroup = oWeb.Groups["<<group name>>"];
// Get the role definition we want to assign ex: Full Control
SPRoleDefinition oRole = oWeb.RoleDefinitions["<< role name>>"];
// Create the role assignment object
SPRoleAssignment oRoleAssignment = new SPRoleAssignment(oGroup);
// Add the role definition to the role assignemnt.
// This will assign the specific permission to the security principal for this role assignemnt.
oRoleAssignment.RoleDefinitionBindings.Add(oRole);
// Now we need to add the role assignment to the web
oWeb.RoleAssignments.Add(oRoleAssignment);
// Now update the web
oWeb.Update();
Enjoy!!!
Posted
09-30-2007 5:13 PM
by
ethan