Guest User

Untitled

a guest
Dec 10th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. SPSecurity.RunWithElevatedPrivileges(delegate()
  2. {
  3. using(SPSite spSite = new SPSite(siteUrl))
  4. {
  5. using(SPWeb spWeb = spSite.OpenWeb())
  6. {
  7. spWeb.AllowUnsafeUpdates = true;
  8. for (int i =0; i < userRequest.Length; i++)
  9. {
  10. SPGroup spGroup = spWeb.SiteGroups[userRequest.GetValue(i).ToString() + "_Contribute"];
  11. spGroup.AddUser(selectedUser); //this works just fine
  12. }
  13. try
  14. {
  15. SPGroupCollection userCollection = selectedUser.Groups;
  16. foreach(SPGroup group in userCollection)
  17. {
  18. if(group.Name == spWeb.SiteGroups[userRequest.GetValue(i) + "_Read"].ToString())
  19. {
  20. group.RemoveUser(selectedUser); //this is where I get Access Denied and a catch in my method if the user is not a Site Admin when the code is ran
  21. }
  22. }
  23. catch (Exception ex)
  24. {
  25. Console.WriteLine(ex.InnerException);
  26. }
  27. }
  28. }
  29. });
Add Comment
Please, Sign In to add comment