SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPSite spSite = new SPSite(Page.Request.Url.ToString()))
{
using (SPWeb spWeb = spSite.OpenWeb())
{
try
{
spWeb.AllowUnsafeUpdates = true;
SPUser spUser = spWeb.EnsureUser(“Text\mossadmin”);//根据登陆名称获取用户对象
if (spUser != null)
{
SPGroup spGroup = spWeb.Groups[“测试组”];//根据用户组名称获取用户组对象
if (spGroup != null)
spGroup.AddUser(spUser);//将用户添加到用户组中
}
}
catch (Exception ex)
{
}
finally
{
spWeb.AllowUnsafeUpdates = false;
}
}
}
});