c# - How to add users to groups under Sub site's in sharepoint sitecollection? -
i have 2 sub-sites in sharepoint site,samplesite1 , 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 sites have 2 groups superuser , 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 subsite's groups normaluser,can use same sharepoint context above siteurl access , perform operations(add/remove user) in groups present under subsites?
if yes,how can it?i have built code add or remove user sharepoint site group based on 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(); }
for subsites can proceed 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 work either sites , 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(); }
Comments
Post a Comment