我对c#和sharepoint都很陌生,并且有一段非常基本的代码,表现得很意外。我显然缺乏一些基本的故障排除技巧,希望有人能指出我正确的方向。
我有一个带有一个按钮和一个标签的应用程序页面。我在页面加载事件中有一些代码可以从外部列表中检索一些信息。我在按钮中有一个完整的代码副本,它返回一个错误。我想知道为什么当pageload事件中的相同代码看起来很好而花花公子时按钮返回错误。
C#代码:
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Text;
namespace farmBDCProj2.Layouts.farmBDCProj2
{
public partial class TestGetInfo : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder output = new StringBuilder();
output.Append("<h2>Generated by PageLoad event</h2>");
SPContext context = SPContext.Current;
using(SPSite site = context.Site)
using (SPWeb web = site.AllWebs["BDC_SQL"])
{
SPList list = web.Lists["BDC_SQL"];
foreach (SPListItem item in list.Items)
{
output.AppendFormat("<br>item: {0}", item["ADName"]);
}
}
Label1.Text = output.ToString();
}
public void btn_click(object sender, EventArgs e)
{
StringBuilder output = new StringBuilder();
output.Append("<h2>Generated by button click event</h2>");
SPContext context = SPContext.Current;
using (SPSite site = context.Site)
using (SPWeb web = site.AllWebs["BDC_SQL"])
{
SPList list = web.Lists["BDC_SQL"];
foreach (SPListItem item in list.Items)
{
output.AppendFormat("<br>item: {0}", item["ADName"]);
}
}
Label1.Text = output.ToString();
}
}
}
ASPX页面:
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestGetInfo.aspx.cs" Inherits="farmBDCProj2.Layouts.farmBDCProj2.TestGetInfo" DynamicMasterPageFile="~masterurl/default.master" %>
<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<asp:Button ID="Button1" runat="server" Text="Click Here" OnClick="btn_click" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</asp:Content>
<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Application Page
</asp:Content>
<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
My Application Page
</asp:Content>
我得到的错误是在btn_click事件的'foreach'阶段,错误文本如下:
Microsoft.SharePoint.SPException was unhandled by user code
Message=Access denied by Business Data Connectivity.
Source=Microsoft.SharePoint
ErrorCode=-2146232832
StackTrace:
at Microsoft.SharePoint.SPListDataSource.GetEntityInstanceEnumerator(XmlNode xnMethodAndFilters)
at Microsoft.SharePoint.SPListDataSource.GetFilteredEntityInstancesInternal(XmlDocument xdQueryView, Boolean fFormatDates, Boolean fUTCToLocal, String firstRowId, Boolean fBackwardsPaging, String& bdcidFirstRow, String& bdcidNextPageRow, List`1& lstColumnNames, Dictionary`2& dictColumnsUsed, List`1& mapRowOrdering, List`1& lstEntityData)
at Microsoft.SharePoint.SPListDataSource.GetFilteredEntityInstances(XmlDocument xdQueryView, Boolean fFormatDates, Boolean fUTCToLocal, String firstRowId, Boolean fBackwardsPaging, String& bdcidFirstRow, String& bdcidNextPageRow, List`1& lstColumnNames, Dictionary`2& dictColumnsUsed, List`1& mapRowOrdering, List`1& lstEntityData)
at Microsoft.SharePoint.SPListItemCollection.EnsureEntityDataViewAndOrdering(String& bdcidFirstRow, String& bdcidNextPageFirstRow)
at Microsoft.SharePoint.SPListItemCollection.EnsureListItemsData()
at Microsoft.SharePoint.SPListItemCollection.get_Count()
at Microsoft.SharePoint.SPEntityInstanceEnumerator.System.Collections.IEnumerator.MoveNext()
at farmBDCProj2.Layouts.farmBDCProj2.TestGetInfo.btn_click(Object sender, EventArgs e)
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException: Microsoft.BusinessData.Infrastructure.AccessDeniedException
Message=Access denied by Business Data Connectivity.
Source=Microsoft.SharePoint
StackTrace:
at Microsoft.SharePoint.BusinessData.Runtime.DataClassRuntime.ExecuteInternal(IDataClass thisDataClass, ILobSystemInstance lobSystemInstance, ILobSystem lobSystem, IMethodInstance methodInstanceToExecute, IMethod methodToExecute, IParameterCollection nonReturnParameters, Object[]& overrideArgs)
at Microsoft.SharePoint.BusinessData.Runtime.EntityRuntime.ExecuteInternal(IDataClass thisDataClass, ILobSystemInstance lobSystemInstance, ILobSystem lobSystem, IMethodInstance methodInstanceToExecute, IMethod methodToExecute, IParameterCollection nonReturnParameters, Object[]& overrideArgs, IFilterCollection filters)
at Microsoft.SharePoint.BusinessData.Runtime.EntityRuntime.ExecuteFiltered(IEntity this, IFilterCollection filterCollection, IMethodInstance methodInstanceToExecute, ILobSystemInstance lobSystemInstance)
at Microsoft.SharePoint.BusinessData.Runtime.EntityRuntime.<FindFiltered>b__3(IEntity e, IMethodInstance mi, IFilterCollection fc, ILobSystemInstance lsi)
at Microsoft.SharePoint.BusinessData.Runtime.EntityInstanceEnumeratorFactory.CreateEntityInstanceEnumerator(IEntity entity, IMethodInstance methodInstance, IFilterCollection filters, ILobSystemInstance lobSystemInstance, ExecutionCallBack executionCallBack)
at Microsoft.SharePoint.BusinessData.MetadataModel.Static.Entity.FindFiltered(IFilterCollection filterCollection, String finderName, ILobSystemInstance lobSystemInstance)
at Microsoft.SharePoint.SPListDataSource.GetEntityInstanceEnumerator(XmlNode xnMethodAndFilters)
InnerException:
非常感谢任何和所有帮助。
谢谢,马特。
答案 0 :(得分:1)
正如ScottE所提到的,您已经处理了由SPContext提供的Web和Site实例。那是错的。你可能不会丢弃它们。
在另一个网站上,您经常执行代码方式。看看ASP.NET生命周期。在回发(按钮单击)的情况下,您的逻辑在加载方法(Page_Load_Stage)和单击处理程序(Event_stage)内执行。除了这两件事之外,您还要确保浏览到此ApplicationPage的用户至少具有对此外部内容类型的读取权限。
例外情况说,当前用户无权读取与BDC_SQL列表关联的请求外部内容类型。
和Thorsten
答案 1 :(得分:0)