The following code will create a site based on a template, then it will add a new contributor group:
//create the subsite .. subSite is an SPWeb object SPWeb createdWeb = subSite.Webs.Add("Url", "Title", "Project Subsite", 1033, "Template Name", true, false); createdWeb.BreakRoleInheritance(true); SPMember member = createdWeb.Users[createdWeb.Author.LoginName]; //create the user groups ... createdWeb.SiteGroups.Add(createdWeb.Title + " Contributors", member, createdWeb.Author, "Contributors to the site"); SPGroup newContribGroup = createdWeb.SiteGroups[createdWeb.Title + " Contributors"]; SPRoleDefinition contribRole = createdWeb.RoleDefinitions.GetByType(SPRoleType.Contributor); SPRoleAssignment contribRoleAssignment = new SPRoleAssignment(newContribGroup); contribRoleAssignment.RoleDefinitionBindings.Add(contribRole); createdWeb.RoleAssignments.Add(contribRoleAssignment);
The above code will break the permission inheritance, so the created site will have unique permissions, this code could be refactored to also create Owners and Reader’s groups.