【发布时间】:2018-08-10 12:42:50
【问题描述】:
我是 Sharepoint 的新手,在参考 url - Access SharePoint online using client object model- Forbidden error 后,我成功地连接到 sharepoint 并在控制台应用程序 C# 中检索详细信息。我的问题是当我将相同的代码移动到一个 asp.net 网站时,它给了我 Collection has not been initialized 错误。这是代码
using (ClientContext clientContext = new ClientContext("https://.../sites/.../"))
{
SecureString passWord = new SecureString();
foreach (char c in "abcdefghijklmnop".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("myname@mydomain.com", passWord);
List list = clientContext.Web.Lists.GetByTitle("ABC DEF");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><RowLimit>1200</RowLimit></View>";
ListItemCollection collListItem = list.GetItems(camlQuery);
clientContext.Load(collListItem, li => li.Include(
pi => pi.Id,
pi => pi["Title"],
pi => pi["Application"],
pi => pi["URL"],
pi => pi["AIR_x0020_ID"]
));
clientContext.ExecuteQuery();
List<string> listofapplications = new List<string>();
foreach (ListItem oListItem in collListItem)
{
{ Console.WriteLine(oListItem["Title"] + ", Application " + oListItem["Application"]); }
}
Console.ReadLine();
}
它给了我 foreach(Listitem oListItem in collListItem) 的错误:
“Microsoft.SharePoint.Client.CollectionNotInitializedException”类型的异常 发生在 Microsoft.SharePoint.Client.Runtime.dll 但不是 在用户代码中处理
附加信息:集合尚未初始化。它 未被请求或请求未被执行。它可能 需要明确要求。
这里是详细的错误:
Microsoft.SharePoint.Client.CollectionNotInitializedException 是 用户代码未处理 HResult=-2146233079 Message=集合 尚未初始化。尚未请求或请求已 没有被执行。可能需要明确请求。
来源=Microsoft.SharePoint.Client.Runtime StackTrace: 在 Microsoft.SharePoint.Client.ClientObjectCollection`1.d__0.MoveNext() 在 c:\Users\rakesh.a.srivastava\Documents\Visual Studio 中的 Home.btn_Click(Object sender, EventArgs e) 2015\WebSites\DNSProvisioningPortal\Home.aspx.cs:第 45 行 在 System.Web.UI.WebControls.Button.OnClick(EventArgs e) 在 System.Web.UI.WebControls.Button.RaisePostBackEvent(字符串 eventArgument) 在 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 事件参数) 在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,字符串 eventArgument) 在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 在 System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,布尔 includeStagesAfterAsyncPoint)
内部异常:
请注意,我在控制台应用程序中没有任何问题,但 asp.net 按钮单击操作。我还添加了 Client 和 Client.Runtime 引用!我无法理解为什么它在控制台中有效,但在网站中无效。我是否缺少任何命名空间,参考?我已经引用了这些 URL The collection has not been initialized、Sharepoint field has not been initialized in C#、Sharepoint Client Object Model The property or field has not been initialized,但没有任何关于我的问题的信息。请帮忙。谢谢
【问题讨论】:
标签: c# asp.net sharepoint office365 sharepoint-online