[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);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s