通过代码方式从SharePoint组中移除用户(代码)
我们可以通过编码的方式从一个SharePoint的组中移除一个用户。下面有两种方式可以从一个SharePoint的组中移除用户。
方式一,
using (SPSite siteCollection = SPContext.Current.Site)
{
using (SPWeb site = siteCollection.OpenWeb())
{
site.AllowUnsafeUpdates = true;
SPGroupCollection collGroups = site.SiteGroups;
SPUserCollection usercoll = siteCollection.RootWeb.SiteUsers;
int ID = site.Users["loginusername"].ID;//domain\username,而非显示名称
usercoll.RemoveByID(ID);
site.Update();
site.AllowUnsafeUpdates = false;
}
{
using (SPWeb site = siteCollection.OpenWeb())
{
site.AllowUnsafeUpdates = true;
SPGroupCollection collGroups = site.SiteGroups;
SPUserCollection usercoll = siteCollection.RootWeb.SiteUsers;
int ID = site.Users["loginusername"].ID;//domain\username,而非显示名称
usercoll.RemoveByID(ID);
site.Update();
site.AllowUnsafeUpdates = false;
}
方式二,
using (SPSite siteCollection = SPContext.Current.Site)
{
using (SPWeb site = siteCollection.OpenWeb())
{
site.AllowUnsafeUpdates = true;
SPGroupCollection collGroups = site.SiteGroups;
SPUserCollection usercoll = siteCollection.RootWeb.SiteUsers;
String LoginID = site.CurrentUser.LoginName;
usercoll.Remove(LoginID);
site.Update();
site.AllowUnsafeUpdates = false;
}
{
using (SPWeb site = siteCollection.OpenWeb())
{
site.AllowUnsafeUpdates = true;
SPGroupCollection collGroups = site.SiteGroups;
SPUserCollection usercoll = siteCollection.RootWeb.SiteUsers;
String LoginID = site.CurrentUser.LoginName;
usercoll.Remove(LoginID);
site.Update();
site.AllowUnsafeUpdates = false;
}