Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two sub-sites in my sharepoint site,SampleSite1 and SampleSite2.
Each of the Sites have two groups superUser and NormalUser. I have given my sharepoint context credentials in appsettings of config file along with SiteUrl

http://LeslieJames.sharepoint.com/sites/MainSite/

//DummyUrl

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 in groups present under subsites?

If Yes, can u share the logic.I already have built code to add or remove user from a sharepoint site group based on some requirements.

C#
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();
Posted
Updated 28-Jan-16 1:23am
v4
Comments
Sinisa Hajnal 28-Jan-16 10:39am    
You don't have to do anything special...subsites inherit access rights from the parent unless you broke inheritance. If you did, add the original parent groups back into your subsite groups, but it would be better to just restore inheritance if you need it.
Member 1097736 28-Jan-16 11:31am    
Hi sinisa,thanks for response.I don't understand how to access the subsites and its's groups.Can u code a sample.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900