如何将用户添加到 Sharepoint 网站集中子网站下的组?
How to add users to groups under Sub site's in sharepoint sitecollection?
我的共享点站点中有两个子站点,父站点下的 SampleSite1 和 SampleSite2,称为 MainSite。
http://xyz.sharepoint.com/sites/MainSite/ - SiteUrl
http://xyz.sharepoint.com/sites/MainSite/SampleSite1 - Subsite1's Url
http://xyz.sharepoint.com/sites/MainSite/SampleSite2 - Subsite2's Url
每个站点分别有两个组 superUser 和 NormalUser。
凭据使用 MainSite 的 SiteUrl。
SecureString password = new SecureString();
string pwd = "Pass123";
string UserName = "abc@xyz.com";
password = convertToSecureString(pwd);
ClientContext clientContext = new ClientContext("http://xyz.sharepoint.com/sites/MainSite/");
clientContext.Credentials = new SharePointOnlineCredentials(UserName, password);
如果将用户添加到 NormalUser 等子站点的组中,我们可以使用与上述 siteUrl 相同的共享点上下文来访问和执行子站点下存在的组中的操作(add/remove 用户)吗?
如果是,我们该怎么做?我已经构建了代码,可以根据某些要求在 Sharepoint 网站组中添加或删除用户。
public void AddUserToDMSite(string useremail, string securityGroupName)
{
GroupCollection collGroup = SPContext.Web.SiteGroups;
Group oGroup1 = collGroup.GetByName("UserList");
Group oGroup2 = collGroup.GetByName(securityGroupName);
UserCollection oUserCollection1 = oGroup1.Users;
UserCollection oUserCollection2 = oGroup2.Users;
SPContext.Load(oUserCollection1);
SPContext.Load(oUserCollection2);
SPContext.ExecuteQuery();
var uname = oGroup1.Users.GetByEmail(useremail);
var userCheck = oUserCollection2.Where(u => u.Email == useremail).FirstOrDefault();
if (userCheck == null)
{
Microsoft.SharePoint.Client.User oUser2 = oGroup2.Users.AddUser(uname);
}
SPContext.ExecuteQuery();
}
对于子网站,您可以按如下方式进行:
Web oWebsite = clientContext.Web;
clientContext.Load(oWebsite, website => website.Webs);
clientContext.ExecuteQuery();
foreach (Web orWebsite in oWebsite.Webs)
{
AddUserToDMSite(useremail, securityGroupName, orWebSite)
}
并将 AddUserToDMSite 更改为与网站和子网站一起使用:
public void AddUserToDMSite(string useremail, string securityGroupName, Web aWeb)
{
GroupCollection collGroup = aWeb.SiteGroups;
Group oGroup1 = collGroup.GetByName("UserList");
Group oGroup2 = collGroup.GetByName(securityGroupName);
UserCollection oUserCollection1 = oGroup1.Users;
UserCollection oUserCollection2 = oGroup2.Users;
SPContext.Load(oUserCollection1);
SPContext.Load(oUserCollection2);
SPContext.ExecuteQuery();
var uname = oGroup1.Users.GetByEmail(useremail);
var userCheck = oUserCollection2.Where(u => u.Email == useremail).FirstOrDefault();
if (userCheck == null)
{
Microsoft.SharePoint.Client.User oUser2 = oGroup2.Users.AddUser(uname);
}
SPContext.ExecuteQuery();
}
我的共享点站点中有两个子站点,父站点下的 SampleSite1 和 SampleSite2,称为 MainSite。
http://xyz.sharepoint.com/sites/MainSite/ - SiteUrl
http://xyz.sharepoint.com/sites/MainSite/SampleSite1 - Subsite1's Url
http://xyz.sharepoint.com/sites/MainSite/SampleSite2 - Subsite2's Url
每个站点分别有两个组 superUser 和 NormalUser。
凭据使用 MainSite 的 SiteUrl。
SecureString password = new SecureString();
string pwd = "Pass123";
string UserName = "abc@xyz.com";
password = convertToSecureString(pwd);
ClientContext clientContext = new ClientContext("http://xyz.sharepoint.com/sites/MainSite/");
clientContext.Credentials = new SharePointOnlineCredentials(UserName, password);
如果将用户添加到 NormalUser 等子站点的组中,我们可以使用与上述 siteUrl 相同的共享点上下文来访问和执行子站点下存在的组中的操作(add/remove 用户)吗?
如果是,我们该怎么做?我已经构建了代码,可以根据某些要求在 Sharepoint 网站组中添加或删除用户。
public void AddUserToDMSite(string useremail, string securityGroupName)
{
GroupCollection collGroup = SPContext.Web.SiteGroups;
Group oGroup1 = collGroup.GetByName("UserList");
Group oGroup2 = collGroup.GetByName(securityGroupName);
UserCollection oUserCollection1 = oGroup1.Users;
UserCollection oUserCollection2 = oGroup2.Users;
SPContext.Load(oUserCollection1);
SPContext.Load(oUserCollection2);
SPContext.ExecuteQuery();
var uname = oGroup1.Users.GetByEmail(useremail);
var userCheck = oUserCollection2.Where(u => u.Email == useremail).FirstOrDefault();
if (userCheck == null)
{
Microsoft.SharePoint.Client.User oUser2 = oGroup2.Users.AddUser(uname);
}
SPContext.ExecuteQuery();
}
对于子网站,您可以按如下方式进行:
Web oWebsite = clientContext.Web;
clientContext.Load(oWebsite, website => website.Webs);
clientContext.ExecuteQuery();
foreach (Web orWebsite in oWebsite.Webs)
{
AddUserToDMSite(useremail, securityGroupName, orWebSite)
}
并将 AddUserToDMSite 更改为与网站和子网站一起使用:
public void AddUserToDMSite(string useremail, string securityGroupName, Web aWeb)
{
GroupCollection collGroup = aWeb.SiteGroups;
Group oGroup1 = collGroup.GetByName("UserList");
Group oGroup2 = collGroup.GetByName(securityGroupName);
UserCollection oUserCollection1 = oGroup1.Users;
UserCollection oUserCollection2 = oGroup2.Users;
SPContext.Load(oUserCollection1);
SPContext.Load(oUserCollection2);
SPContext.ExecuteQuery();
var uname = oGroup1.Users.GetByEmail(useremail);
var userCheck = oUserCollection2.Where(u => u.Email == useremail).FirstOrDefault();
if (userCheck == null)
{
Microsoft.SharePoint.Client.User oUser2 = oGroup2.Users.AddUser(uname);
}
SPContext.ExecuteQuery();
}