2

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.

1
  • 1
    What is the exception? Also what are you doing to the list that requires an update to it?
    – Mike Polen
    Commented Sep 22, 2012 at 10:23

3 Answers 3

2

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**
       }
    }
});
2
  • i tried but same issue Commented Sep 22, 2012 at 10:17
  • 1
    How do you retrieve the list? you should use the web wich is created in the delegate method.
    – Alexander
    Commented Sep 22, 2012 at 21:29
2

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/

0

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;

            });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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