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