Tuesday, 2 September 2008

Adding Groups and Users to a SharePoint Site in code

There are plenty of blogs around about adding users to groups, but i have found there are not many explaining the end to end code for the creation of a new group in a site, assigning the permissions then adding of users to that group, so here goes.

Firstly, when you add a group to a site, it is not just the simple case of using the SPGroupCollection add method of the SPWeb object,  you need to follow this up by setting the RoleAssignments and RoleDefinitionsBindings.

The code below is using an application page, therefore using the LayoutsPage context to get the SPWeb object using this.Web.   If you are using this in a web part or other code, then you would wrap this inside a using(SPWeb) or a try catch scoped SPWeb object.

We start by checking to see if the site is inheriting its permissions and breaking if required. In this example, the existing inherited group permissions are copied across when breaking the inheritance by using the true parameter.

Code Snippet
  1. using (SPSite site = new SPSite("http://mysite.com/sitecollection"))
  2. {
  3.     using (SPWeb web = site.OpenWeb("mysitename"))
  4.     {
  5.         if (!web.HasUniqueRoleAssignments)
  6.         {
  7.             //use the true parameter to copy the permissions
  8.             //when breaking inheritance
  9.             web.BreakRoleInheritance(true);
  10.         }
  11.     }
  12. }

Then we need to create the new group by adding in a site owner and default member.  Note: this alone will not create the group in the site, the RoleAssignment and RoleDefinitions need to be added below.

Code Snippet
  1. PeopleEditor peApproversGroup;
  2. using (SPSite site = new SPSite("http://mysite.com/sitecollection"))
  3. {
  4.     using (SPWeb web = site.OpenWeb("mysitename"))
  5.     {
  6.         if (!web.HasUniqueRoleAssignments)
  7.         {
  8.             //use the true parameter to copy the permissions
  9.             //when breaking inheritance
  10.             web.BreakRoleInheritance(true);
  11.             //Create the group then get a Group Object reference to the created group
  12.             web.SiteGroups.Add("Group Name", web.CurrentUser, web.CurrentUser, "Site Group Description");
  13.             SPGroup newGroup = web.SiteGroups["Group Name"];
  14.             //Set properties of the group settings
  15.             newGroup.AllowMembersEditMembership = true;
  16.             //Add in the role assignment and definition
  17.             SPRoleAssignment roleAssignment = new SPRoleAssignment(newGroup);
  18.             //Create object based on Approval role definition
  19.             SPRoleDefinition roleDefinition = web.RoleDefinitions["Approve"];
  20.             roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
  21.             //finally add the role assignment
  22.             web.RoleAssignments.Add(roleAssignment);
  23.             foreach (PickerEntity user in peApproversGroup.Entities)
  24.             {
  25.                 SPUser userToAdd = web.SiteUsers[user.Key];
  26.                 newGroup.AddUser(userToAdd);
  27.             }
  28.             newGroup.Update();
  29.  
  30.  
  31.         }
  32.     }
  33. }

the group should be created, owner assigned and users added, hope this helps.

0 comments:

Post a Comment