Pages

Showing posts with label Access Denied Error. Show all posts
Showing posts with label Access Denied Error. Show all posts

Tuesday, September 21, 2010

“Access Denied” Error in Sharepoint 2007

Hello Friends

“Access Denied” Error in Sharepoint 2007 :

In an application , when I save some of items under users who are belong to a Group which has Read Permission they get the error “Access Denied” when they add, edit or update any List item. Understood!!!

Then I write the code which do the higher operations against their permissions to under the “System Account” privilegaes  by running code inside this code block SPSecurity.RunWithElevatedPrivileges(delegate() ..
Like

SPSecurity.RunWithElevatedPrivileges(delegate()
{
     SPContext.Current.Web.AllowUnsafeUpdates = true;
     //take the lists and make update operation
     SPContext.Current.Web.Update();
}

Still giving me same error “Access Denied”!!!!  A big frustration!!

But after digging I get what we should be under the SPSecurity.RunWithElevatedPrivileges(delegate().

Under SPSecurity.RunWithElevatedPrivileges(delegate() we should never take an object of SPcontext.current.Web

SPSecurity.RunWithElevatedPrivileges(delegate()
{
                   
      using (SPSite oSite = new SPSite(SPContext.Current.Web.Site.Url))
      {
      // creating a new SPSite running under Application pool idenity
                        
            using (SPWeb oWeb = oSite.OpenWeb())
            {
                oWeb.AllowUnsafeUpdates = true;

                  // Code
            }

It runs like a charm!!!
                               
Attention:

Not assign any variable by using SPContext.Current.Web rather than use adminWeb for example take list like that inside the block SPSecurity.RunWithElevatedPrivileges(delegate().

Disha Shah