Tags

,

You may get exception if you using RunWithElevatedPrivileges like below

    SPSecurity.RunWithElevatedPrivileges(delegate() {

        SPSite siteColl =    SPContext.Current.Site;
        SPWeb site = SPContext.Current.Web;
     //Code to execute

    });

That’s because SPSite object permissions are determined when they are created, so SPContext.Current.Site will already have the permissions of the current user even if you get the reference within RWEP.

The code should look like below:

SPSite siteColl = SPContext.Current.Site;
SPWeb site = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate() {
  using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID)) {
    using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(site.ID)) {
        //Code to execute
    }
  }
});
Advertisements