8

I want regular user can access the "User Information List" in Mysite root site. I am using "RunWithElevatedPrivileges" method. Still throwing access denied error. per example my root site collection for mysite is "http://network.test.com". the user want assess userinformation list this site collection. How can he access that?

 SPSecurity.RunWithElevatedPrivileges(delegate
 {
   using (SPSite site = new SPSite(SPContext.Current.Web.Site.ID))
   {

   ServerContext sc = ServerContext.Current;
   UserProfileManager upm = new UserProfileManager(sc);
   UserProfile up = null;
   //get current user's profile (visitor)
   if (upm.UserExists(SPContext.Current.Web.CurrentUser.LoginName))
   {
       up =upm.GetUserProfile(SPContext.Current.Web.CurrentUser.LoginName);

      SPWeb web = SPContext.Current.Web;
      SPList userInformationList = web.Lists["User Information List"];
James123
  • 11,184
  • 66
  • 189
  • 343

3 Answers3

6

SPContext.Current runs outside the RunWithelevatedPrivileges elevated context. For more info see this blog post.

Colin
  • 10,630
  • 28
  • 36
6

You're setting your SPWeb to SPContext.Current.Web, this doesn't have elevated privileges. Only SPWebs created from SPSites created inside the delegate are elevated.

So you need to replace

SPWeb web = SPContext.Current.Web;

with

SPWeb web = site.OpenWeb(SPContext.Current.Web.ID);
Per Jakobsen
  • 3,767
  • 17
  • 19
-1

You're setting your SPWeb to SPContext.Current.Web this doesn't have elevated privileges. Refer this post:

Engineer
  • 1,436
  • 3
  • 18
  • 33
Sivakumar Piratheeban
  • 493
  • 4
  • 11
  • 39