Search code examples
c#asp.netsharepointsharepoint-2010csom

How to add users to groups under Sub site's in sharepoint sitecollection?


I have two sub-sites in my sharepoint site,SampleSite1 and SampleSite2 under Parentsite called 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

Each of the Sites have two groups superUser and NormalUser respectively.

The credential uses SiteUrl of MainSite.

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

Incase of adding user to subsite's groups like NormalUser,Can we use the same sharepoint context with above siteUrl to access and perform operations(add/remove user) in groups present under subsites?

If Yes,how can we do it?I already have built code to add or remove user from a sharepoint site group based on some requirement.

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

Solution

  • For subsites you can proceed as follows:

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

    and change AddUserToDMSite to work with either sites and subsites as:

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