SharePoint 2010 - System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

 

I am sure many would have experienced this issue while connecting to a SharePoint site from an Asp.Net application. You normally would get these errors:

1 . System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) at Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(Exception ex) at Microsoft.SharePoint.Library.SPRequest.GetListsWithCallback(String bstrUrl, Guid foreignWebId, String bstrListInternalName, Int32 dwBaseType, Int32 dwBaseTypeAlt, Int32 dwServerTemplate, UInt32 dwGetListFlags, UInt32 dwListFilterFlags, Boolean bPrefetchMetaData, Boolean bSecurityTrimmed, Boolean bGetSecurityData, Boolean bPrefetchRelatedFields, ISP2DSafeArrayWriter p2DWriter, Int32& plRecycleBinCount) at Microsoft.SharePoint.SPListCollection.EnsureListsData(Guid webId, String strListName) at Microsoft.SharePoint.SPListCollection.GetListByName(String strListName, Boolean bThrowException) at _Default.btnClick_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\Contoso\Default.aspx.cs:line 35

2. The Web application at https://localhost/sites/SPApplication101/ could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

However, if you try to access the same SharePoint application from a console application or windows application (provided the target framework is Any CPU or x64; because, SharePoint 2010 runs in x64 mode only).

Now, there could be following reasons behind these errors.

1. Your ASP.NET application may be targeting to x86 framework and not Any CPU or x64.

2. The user account which is running the ASP.NET application may not have access to SharePoint application.

3. The application pool identify of the application pool of the ASP.NET application doesn’t have the access to SharePoint.

The solution to the above:

1. Make sure your ASP.NET application is targeting x64/Any CPU.

2. Run your code inside the block “SPSecurity.RunWithElevatedPrivileges”.

3. Make sure the application identity of the app pool of ASP.NET application has the access to the SharePoint application which your accessing. Alternatively, you can use the SharePoint’s application with your ASP.NET application.

try

        {

            SPSecurity.RunWithElevatedPrivileges(delegate()

            {

                SPSite mySite = new SPSite("https://localhost/sites/SPApplication101/");

                using (SPWeb myWeb = mySite.OpenWeb())

                {

                    int i = 1;

                    string strFolder = "Folder";

                    mySite.AllowUnsafeUpdates = true;

                    myWeb.AllowUnsafeUpdates = true;

                    SPDocumentLibrary docLibrary = (SPDocumentLibrary)myWeb.Lists["Word Docs"];

                    SPFolderCollection myFolders = myWeb.Folders;

                    if (docLibrary != null)

                    {

                        while (i <= 10)

                        {

                            strFolder = strFolder + " " + i.ToString();

                            if (!myWeb.GetFolder(strFolder).Exists)

                            {

                                myFolders.Add(docLibrary.ParentWebUrl + "/Word%20Docs/" + strFolder + "/");

                                docLibrary.Update();

                            }

                            strFolder = "Folder";

                            i++;

                        }

                    }

                }

            });

        }

        catch (Exception ex)

        {

            Response.Write(ex.ToString());

        }

-------------

Hope this helps many! Smile

Comments

  • Anonymous
    July 11, 2012
    thank you so much... this helped my permission issue deleting terms in the managed metadata term store with an event handler. Much love.

  • Anonymous
    November 07, 2012
    Thank you very much Vijay-MSFT, ive been trying to resolve these problems the entire day Thank you :)

  • Anonymous
    April 18, 2013
    We can also impersonate user, to access SP Object and do the Task. below is the Code. This is very helpful post, just need to add some if it is helpful.  private static void impersonateTest(string url)        {            string siteStr = url;// "http://mysharepointsite/";            SPSite tempSite = new SPSite(siteStr);            SPUserToken systoken = tempSite.SystemAccount.UserToken;            using (SPSite site = new SPSite(siteStr, systoken))            {                using (SPWeb web = site.OpenWeb())                { I need to add the workflow history to my site                   // web.Lists.Add("Workflow History", string.Empty, SPListTemplateType.WorkflowHistory);                                      //right now, logged in as Site System Account                    Console.WriteLine("Currently logged in as: " +                                       web.CurrentUser.ToString());                    switchUser(web, siteStr, "win-m9n2ht2m8ef/aziz");                }            }        }        private static void switchUser(SPWeb web, string siteStr, string user)        {            //impersonate somebody else            SPUserToken userToken = web.AllUsers[user].UserToken;            SPSite s = new SPSite(siteStr, userToken);            SPWeb w = s.OpenWeb();            Console.WriteLine("Currently logged in as: " +                              w.CurrentUser.ToString() +                              "(" + w.CurrentUser.Name + ")"                             );        }

  • Anonymous
    October 09, 2015
    Excellent! Fairly simple, solves the issue (for me at least), and doesn't involve potentially confusing and ineffective configuration changes. Thank you!

  • Anonymous
    February 10, 2016
    In my case the share-point is working but by accident IIS Server is touched. Central administrator application pool stops. tried all the scenarios and finally launched sharepoint configuration wizard and i get the following error