Tuesday, September 6, 2011

How to remove users from SharePoint groups through code

 

You can create a variable to store the current user(who created) the survey item.

then create a method 

 
public void removeuser(string groupName, string userLoginName)
{
string strUrl = "http://mysite:5050/";
using (SPSite site = new SPSite(strUrl))
{
using (SPWeb web = site.OpenWeb())
{
try
{
web.AllowUnsafeUpdates = true;
SPUser spUser = web.AllUsers[userLoginName];
{
SPGroup spGroup = web.Groups[groupName];
if (spGroup != null)
spGroup.RemoveUser(spUser);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
web.AllowUnsafeUpdates = false;
}
}
}
}



 




call this method and pass the group name & "variable" created earlier.

Note :Replace "http://mysite:5050/" with your site URL

No comments:

Post a Comment