Sunday, January 18, 2009

How To Add Users to a Sharepoint Group in c#

public void AddUserToSPGroup(String userName,string group)
{
try
{
SPWeb web = site.AllWebs[ConfigurationManager.AppSettings["WEB_SITE"].ToString()];

//SPUser spUser = web.AllUsers[domain + "\\" + userName];
SPUser spUser = web.AllUsers["ad:"+ userName];

//Open group
SPGroup spGroup = web.SiteGroups[group];

//Add and update group with new user
web.AllowUnsafeUpdates = true;
spGroup.AddUser(spUser.LoginName, spUser.Email, spUser.Name, "Added by UserControl");
spGroup.Update();
}
catch(Exception ex)
{
//Response.Write(ex.Message);
lblUserList.Text =ex.Message.ToString();
}
}

No comments: