//Create a group and Role permissionsSPSite site = new SPSite("http://Madan:8181");
SPWeb web = site.OpenWeb();web.AllowUnsafeUpdates =
true;
try{
SPMember siteOwner = web.SiteAdministrators[0];
//Assign the group name string grpName = "MVMMohanGroup";
//check if group exists bool grpExists = false;
foreach (SPGroup group in web.SiteGroups)
if (group.Name.ToLower() == grpName.ToLower())grpExists =
true;
if (!grpExists){
web.SiteGroups.Add(grpName, siteOwner,
null, "Eve Custom Group that I created because I have the permissions!");web.Update();
//get a reference to the group we just createdSPGroup customGroup = web.SiteGroups[grpName];
//add the group to the quicklaunchweb.Properties["vti_associategroups"] = web.Properties["vti_associategroups"] + ";" + customGroup.ID.ToString();
//Assign the roles for itSPGroup Role2Group = web.SiteGroups[grpName];
//Get the designer role definition to assign to our custom group any custom role alsoSPRoleDefinition mydesignerRoleDefinition = web.RoleDefinitions["View Only"];
//assign designer role to our custom group at the web levelSPRoleAssignment roleAssignment = new SPRoleAssignment(Role2Group);roleAssignment.RoleDefinitionBindings.Add(mydesignerRoleDefinition);
web.RoleAssignments.Add(roleAssignment);
customGroup.Update();
web.Properties.Update();
Response.Write(
"Group is created.");}
else{
Response.Write("Group is already created.");}
}
catch (Exception ex){
Response.Write(
"Group is not created." + ex.Message);}