如何将用户添加到sharepoint sitecollection中子站点下的组?

时间:2016-01-28 11:07:26

标签: c# asp.net sharepoint sharepoint-2010 csom

我的sharepoint网站上有两个子网站,名为MainSite的在Parentsite下的SampleSite1和SampleSite2。

  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相同的共享点上下文来访问子网站下的组中的操作(添加/删除用户)?

如果是,我们该怎么做?我已经构建了代码,可以根据某些要求在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(); 
     }

1 个答案:

答案 0 :(得分:1)

对于子网站,您可以按以下步骤操作:

var G = [12, 34, 5];

G.push(G.label = 567856);
G.push(G.other = Infinity);

G.forEach(function(val) {
  console.log(val); // 12 ... 34 ... 5 ... 567856 ... Infinity
});

console.log(G.label); //567856
console.log(G.other); //Infinity

Object.defineProperty(G, 'label', {
  set: function(x) {
    this[3] = x;
  }
});

Object.defineProperty(G, 'other', {
  set: function(x) {
    this[4] = x;
  }
})

G.label = 99999;
G.other = 11111;

G.forEach(function(val) {
  console.log(val); // 12 ... 34 ... 5 ... 99999 ... 11111
});

并更改AddUserToDMSite以使用网站和子网站:

Web oWebsite = clientContext.Web;
clientContext.Load(oWebsite, website => website.Webs);
clientContext.ExecuteQuery();
foreach (Web orWebsite in oWebsite.Webs)
{
    AddUserToDMSite(useremail, securityGroupName, orWebSite)
}