【问题标题】:How to add users to groups under Sub site's in sharepoint sitecollection?如何将用户添加到共享点网站集中子网站下的组?
【发布时间】:2016-01-30 07:15:52
【问题描述】:

我的共享点站点中有两个子站点,父站点下的 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 相同的共享点上下文来访问和执行子站点下存在的组中的操作(添加/删除用户)吗?

如果是,我们该怎么做?我已经构建了代码来根据某些要求从共享点站点组中添加或删除用户。

 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(); 
     }

【问题讨论】:

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


    【解决方案1】:

    对于子网站,您可以按以下方式进行:

    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(); 
     }
    

    【讨论】:

    • 感谢您的回复马可。将参数传递给方法时出现错误 - 非静态字段、方法或属性“SharepointTestApp.Program.AddUserToDMSite(string, string, Microsoft.SharePoint.Client.Web)”需要对象引用
    • 是否可以将对象作为参数发送?
    • 嗨,当然可以将对象作为参数传递。请尝试将方法 AddUserToDMSite 设置为静态并让我知道结果。公共静态无效 AddUserToDMSite(...
    猜你喜欢
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多