During one of our projects we needed to create a new SiteGroup, add users to it and define some roles.The following code can be used for adding a SiteGroup to a site in WSS or MOSS.
SPWeb currentWeb = SPContext.Current.Web;
SPUser currentUser = currentWeb.CurrentUser;
currentWeb.SiteGroups.Add("MyGroup", currentUser, currentUser, "My new group");
Then we need to get the group based on its name and add the roles to it based on the names of the roles.
SPGroup group = currentWeb.SiteGroups["MyGroup"];
string[] roleNames = new string[] { "reader", "contributor", "approver" };
foreach(string roleName in roleNames)
{
currentWeb.Roles[roleName].AddGroup(group);
}
Finally we need to add some user to the group. This is realised by adding a SPUser object to the group itself.
group.AddUser(someUser);