Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Thursday, December 01, 2011

Add User to a selected group in SharePoint

SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb currentWeb = SPControl.GetContextWeb(Context);
using (SPSite site = new SPSite(currentWeb.Url))
{
using (SPWeb objWeb = site.OpenWeb())
{
objWeb.AllowUnsafeUpdates = true;
SPUser spUser = objWeb.EnsureUser(peAddUser.Accounts[0].ToString());
if (spUser != null)
{
SPGroup spGroup = objWeb.Groups["Name of Your Group"];
if (spGroup != null)
{
spGroup.AddUser(spUser);
string strUrl=objWeb.Url+"/UserPages/Add User.aspx";
Page.Response.Redirect(strUrl);
}
}
}
}
});

No comments:

Post a Comment