HomeDevelopment (Old) SPSecurity.RunWithElevatedPrivileges informations

[SharePoint 2010 – Developpement Informations] SPSecurity.RunWithElevatedPrivileges informations

SPSecurity.RunWithElevatedPrivileges launch the code with the administrator account of SharePoint.
Thought it could tell you during the execution : “Access denied” or :

UnauthorizedAccessException was unhandled by user code
0x80070005

Make sure you have sufficient privileges to access this resource. If you are attempting to access a file, make sure it is not readonly…

Make sure you re-open a new SPSite inside the SPSecurity.RunWithElevatedPrivileges in order to get the authorization of the administrator.

 

Example in a feature receiver :

 

   1: SPSecurity.RunWithElevatedPrivileges(delegate()
   2:             {
   3:                 using (SPSite site = new SPSite(properties.List.ParentWeb.Site.Url))
   4:                 {
   5:                     using (SPWeb web = site.OpenWeb(properties.List.ParentWeb.Url))
   6:                     {
   7:                         SPList List = properties.List;
   8:
   9:                         try
  10:                         {
  11:                             this.EventFiringEnabled = false;
  12:                             base.ItemAdded(properties);
- Advertisement -

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

CONNECT FOR MORE CONTENT

DO NOT MISS THOSE ARTICLES

Recent Comments