Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

How can I create a new SharePoint group by code?

share|improve this question
add comment

2 Answers

From http://www.codekeep.net/snippets/0301b30a-cdf9-4dd2-bbd0-0be4ad6c1a90.aspx :

using (SPWeb topSite = (SPWeb)properties.Feature.Parent)
    SPGroup grouppi = null;
    SPMember oMember;
    string[] protectedDocumentReaders = ConfigurationManager.AppSettings["$appSetting$"].ToString().Split(new char[] { ';' });
       oMember = topSite.Users[topSite.Author.LoginName];                                        
       grouppi = root.SiteGroups["$listname$"];
    catch { }

    if (grouppi == null)
        root.SiteGroups.Add("$gname$", oMember, root.Author, "My New Group Description");
        grouppi = root.SiteGroups["$gname$"];

        SPRoleDefinition oRole = root.RoleDefinitions.GetByType(SPRoleType.Contributor);
        SPRoleAssignment oRoleAssignment = new SPRoleAssignment(grouppi);

        foreach (string user in protectedDocumentAdmins)
            grouppi.Users.Add(user, "", user, "");
share|improve this answer
add comment

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
add comment

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.