Wow! 3rd blog in almost 2 years. Well here it is...
The code below shows how to create a custom SPGroup and assign appropriate permissions on the site and list
using (SPSite site = new SPSite("http://sitename"))
{
SPWeb web = site.AllWebs[0];
//use the web owner group as the owner for this new group
SPGroup groupOwner = web.SiteGroups.GetByID(int.Parse(web.Properties["vti_associateownergroup"]));
string groupName = "My Custom Group";
web.SiteGroups.Add(groupName, groupOwner, null, "Custom SharePoint Group for Demo");
//retrieve the newly added group to set roles for it
SPGroup wcmGroup = web.SiteGroups[groupName];
//get the designer role definition to assign to our custom group
SPRoleDefinition designerRoleDefinition = web.RoleDefinitions["Design"];
//assign designer role to our custom group at the web level
SPRoleAssignment roleAssignment = new SPRoleAssignment(wcmGroup);
roleAssignment.RoleDefinitionBindings.Add(designerRoleDefinition);
web.RoleAssignments.Add(roleAssignment);
wcmGroup.Update();
SPList customList = web.Lists["CustomList"];
if (!masterPageGallery.HasUniqueRoleAssignments)
{
masterPageGallery.BreakRoleInheritance(true);
}
//we have unique role assignments
//assign reader role to our custom group for the list
SPRoleDefinition readerRoleDef = web.RoleDefinitions.GetByType(SPRoleType.Reader);
roleAssignment = new SPRoleAssignment(wcmGroup);
roleAssignment.RoleDefinitionBindings.Add(readerRoleDef);
customList.RoleAssignments.Add(roleAssignment);
customList.Update();
web.Update();
}
Happy Coding!
