Home
Manage Your Code
Snippet: Create Sharepoint Group (C#)
Title: Create Sharepoint Group Language: C#
Description: Create new Sharepoint group in FeatureActivated Event (for example) Views: 8451
Author: Jukka Kotamäki Date Added: 11/5/2008
Copy Code  
1using (SPWeb topSite = (SPWeb)properties.Feature.Parent)
2{
3    SPGroup grouppi = null;
4    SPMember oMember;
5    string[] protectedDocumentReaders = ConfigurationManager.AppSettings["$appSetting$"].ToString().Split(new char[] { ';' });
6    try
7    {
8        oMember = topSite.Users[topSite.Author.LoginName];                                        
9        grouppi = root.SiteGroups["$listname$"];
10    }
11    catch { }
12
13    if (grouppi == null)
14    {
15        root.SiteGroups.Add("$gname$", oMember, root.Author, "My New Group Description");
16        grouppi = root.SiteGroups["$gname$"];
17
18        SPRoleDefinition oRole = root.RoleDefinitions.GetByType(SPRoleType.Contributor);
19        SPRoleAssignment oRoleAssignment = new SPRoleAssignment(grouppi);
20        oRoleAssignment.RoleDefinitionBindings.Add(oRole);
21        root.RoleAssignments.Add(oRoleAssignment);
22        root.Update();
23
24        foreach (string user in protectedDocumentAdmins)
25        {
26            grouppi.Users.Add(user, "", user, "");
27        }
28
29    }
30}