SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I create a new SharePoint group by code?

share|improve this question
SPWeb root = site.RootWeb;
SPGroup group = null;

// Check if the group exists
    group = root.SiteGroups["The Group Name"];
catch { }

// If it doesn't, add it
if (group == null)
    root.SiteGroups.Add("The Group Name", member, root.Author, "Your Group Description");
    group = root.SiteGroups["The Group Name"];

    // Add the group's permissions
    SPRoleDefinition roleDefinition = root.RoleDefinitions.GetByType(SPRoleType.Contributor);
    SPRoleAssignment roleAssignment = new SPRoleAssignment(group);
share|improve this answer
Is there compulsory to update the root (root.update()) in the end ? – Dipen Shah Dec 30 '15 at 9:58
@DipenShah Try it without and see if it persists :) – RJ Cuthbertson Dec 30 '15 at 21:34

Creating the group is simple just call SPGroupCollection.Add

using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb)
    SPGroupCollection collGroups = oWebsiteRoot.SiteGroups;
    SPUser oUser = oWebsiteRoot.Users["User_Name"];
    SPMember oMember = oWebsiteRoot.Users["User_Name"];

    collGroups.Add("Group_Name", oMember, oUser, "Description");

Then you need may need to add users/groups and permissions depending on your needs

share|improve this answer
The code for creating the group is perfect. But I don't think you should dispose the RootWeb object created from SPContext.Current.Site. Details here:… – Vardhaman Deshpande May 16 '14 at 15:20
RJ Cuthbertson's answer is closer to how I'd want things to work - after creating a group you also want to associate it to a site, and this is done by using the SPRoleDefinition/SPRoleAssignment combo. – Vlad May 10 '15 at 11:47
web.SiteGroups.Add(groupName, web.SiteAdministrators[0], web.SiteAdministrators[0], "");
SPGroup group = web.SiteGroups[groupName];
// Add the group's permissions
SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Administrator);//Add Role Type as Administrator(Full Control),Contributor and Editor etc.
SPRoleAssignment roleAssignment = new SPRoleAssignment(group);

Follow the link below for complete code Create SharePoint Group and Group Permission Programmatically

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.