none
Access denied when executing SPSecurity.RunWithElevatedPrivileges(delegate()

    Question

  • I have some custom application pages that used to work fine in Sharepoint 2007.  Now we are migrating to Sharepoint 2010.  We've already moved all the custom code etc. and right now when the custom code executes and gets to the line SPSecurity.RunWithElevatedPrivileges(delegate()

    then I'm getting "access denied" exception.  Usually, when people get the "access denied" error it's when some code inside the RunWithElevatedPrivileges() block executes.  However, in my case the code does not even enter the first line inside this block.  It break on the RunWithElevatedPrivileges() line. 

    In IIS the account used for the application pool has site collection admin privileges and has server admin privileges.  So what else can be the problem?  Does it have to do with the Sharepoint trust level?  Code access security?  I'm running out of ideas so any suggestions will be greatly appreciated.

    thanks

    Wednesday, October 24, 2012 8:03 PM

Answers

  • It depends on what you are trying to do within the RunWithElevatedPrivileges. In SP2010 this is no longer a guarantee that you can do anything. What exactly is the code you are trying to execute?

    Blog | SharePoint Field Notes Dev Tool | ClassMaster

    Wednesday, October 24, 2012 9:10 PM
  • string siteURL  = "http://site";
    string webURl = "/HR";
    SPSecurity.RunWithElevatedPrivileges(delegate(){
        using(SPSite site = new SPSite(siteURL)){
            using(SPWeb web = site.openWeb(webURL)){
                //You're custom code here
           }
        }
    });

    did you implement it like this ?

    Wednesday, October 24, 2012 9:29 PM

All replies

  • It depends on what you are trying to do within the RunWithElevatedPrivileges. In SP2010 this is no longer a guarantee that you can do anything. What exactly is the code you are trying to execute?

    Blog | SharePoint Field Notes Dev Tool | ClassMaster

    Wednesday, October 24, 2012 9:10 PM
  • string siteURL  = "http://site";
    string webURl = "/HR";
    SPSecurity.RunWithElevatedPrivileges(delegate(){
        using(SPSite site = new SPSite(siteURL)){
            using(SPWeb web = site.openWeb(webURL)){
                //You're custom code here
           }
        }
    });

    did you implement it like this ?

    Wednesday, October 24, 2012 9:29 PM