Adding Permission Level to SharePoint Group using C# Code:
private void SetRoleDefinition(string groupName, string permissionLevel){
SPRoleAssignment roleAssignment = null;
try
{
using (SPWeb oweb =Site.OpenWeb())
{
roleAssignment = new SPRoleAssignment(oweb.SiteGroups[groupName]);
roleAssignment.RoleDefinitionBindings.Add(oweb.RoleDefinitions[permissionLevel]);
oweb.RoleAssignments.Add(roleAssignment);
oweb.AllowUnsafeUpdates = true;
oweb.Update();
oweb.AllowUnsafeUpdates = false;
}
}
catch (Exception e)
{
throw e;
}
finally
{
roleAssignment = null;
}
}
Cehck if Group already exsits or not in sharepoint Site
private bool IsGroupExist(SPGroupCollection groupCollection, string aGroupName)
{
SPGroup Group = null;
try
{
Group = groupCollection[aGroupName];
return true;
}
catch
{
return false;
}
}