SharePoint 2010 - System.UnauthorizedAccessException: Access is denied

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 http://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("http://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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值