Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am trying to update a list item using:

SPSecurity.RunWithElevatedPrivileges(delegate() 
{
    ...

    item.update();**// this line works**
    list.update();**//// exception thrown on this statement**
});

When I update the item, it works fine with no exception. But on updating the list... it throws an exception and redirects me to the Access Denied page.

share|improve this question
1  
What is the exception? Also what are you doing to the list that requires an update to it? –  Mike Polen Sep 22 '12 at 10:23
add comment

3 Answers

you should open new context

SPSecurity.RunWithElevatedPrivileges(delegate() 
{
    using (SPSite site = new SPSite(SPContext.current.Site.ID)
    {
       using (SPWeb web = site.OpenWeb(SPContext.current.Web.ID))
       {
           SPList list = web.Lists["Some List"];
           .....
           item.update();**// this line works**
           list.update();**//// exceptoin throw on this statement**
       }
    }
});
share|improve this answer
    
i tried but same issue –  Muhammad Adnan Sep 22 '12 at 10:17
1  
How do you retrieve the list? you should use the web wich is created in the delegate method. –  Alexander Sep 22 '12 at 21:29
add comment

Please ensure you use SPSecurity.RunWithElevatedPrivileges in correct way. Consider below points for the same:

  • RunWithElevatedPrivileges does not work when HTTPContext is null.
  • RunWithElevatedPrivileges does not work for a Sandbox Solution.

  • Elevation of privilege occurs only if new SPSite created inside the block.

Below is commonly used WRONG way and you may get access denied:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
      SPSite elevatedSite = SPContext.Current.Site;
      using (SPWeb elevatedWeb = elevatedSite.OpenWeb())
      {
         // Performing administrative actions here will give Access Denied exception.
      }
   }
});

CORRECT Way:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
   using (SPSite elevatedSite = new SPSite(SPContext.Current.Site.Id))
   {
      using (SPWeb elevatedWeb = elevatedSite.OpenWeb(SPContext.Current.Web.Id))
      {
          // Perform administrative actions by using the elevated site and web objects. 
          // elevatedWeb.CurrentUser.LoginName gives SHAREPOINTsystem          
      }
   }
});

Please check out this post for more details:

http://extreme-sharepoint.com/2012/05/30/impersonation-elevation-of-privileges/

share|improve this answer
add comment

try this code

    SPSite mysite = SPContext.Current.Site;
    SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPSite mycurrentsite = new SPSite(mysite.ID);
                SPWeb mycurrentweb = mycurrentsite.OpenWeb();
                mycurrentweb.AllowUnsafeUpdates = true;
                SPList list = mycurrentweb .Lists["Some List"];      .....
                  item.update();
                  list.update();
                 mycurrentweb.AllowUnsafeUpdates = false;

            });
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.