Monday, January 19, 2009

How to insert sharepoint user to a sharepoint user Group in c#

using Microsoft.SharePoint;


public void AddUserToSPGroup(String _UserName, string _GroupName)
{
try
{
SPSite site = new SPSite(ConfigurationManager.AppSettings["SITE_URL"].ToString());
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[_GroupName];

//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)
{
lblUserList.Text = ex.Message.ToString();
}
}

No comments: