daily pastebin goal
61%
SHARE
TWEET

Untitled

a guest Dec 10th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top