我是 Sharepoint 的新手,在参考 url - Access SharePoint online using client object model - Forbidden error 后,我成功连接到 sharepoint 并在控制台应用程序 C# 中检索详细信息。我的问题是当我将相同的代码移动到 asp.net 网站时,它给我集合尚未初始化错误。这是代码
using (ClientContext clientContext = new ClientContext("https://.../sites/.../"))
{
SecureString passWord = new SecureString();
foreach (char c in "abcdefghijklmnop".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("[email protected]", 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.Runtime.dll 中出现“Microsoft.SharePoint.Client.CollectionNotInitializedException”类型的异常,但未在用户代码中处理
附加信息:集合尚未初始化。尚未请求或尚未执行请求。可能需要明确请求。
这是详细的错误:
Microsoft.SharePoint.Client.CollectionNotInitializedException 未由用户代码处理 HResult=-2146233079 Message=集合尚未初始化。尚未请求或尚未执行请求。可能需要明确请求。
Source=Microsoft.SharePoint.Client.Runtime StackTrace:在 Microsoft.SharePoint.Client.ClientObjectCollection`1.d__0.MoveNext() 在 Home.btn_Click(Object sender, EventArgs e) in c:\Users\rakesh.a.srivastava\ Documents\Visual Studio 2015\WebSites\DNSProvisioningPortal\Home.aspx.cs:line 45 at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)在 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) 在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) 在 System.Web.UI.Page。 RaisePostBackEvent(NameValueCollection postData) 在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
内部异常:
请注意,我在控制台应用程序中没有遇到任何问题,而是在 asp.net 按钮单击操作中遇到了任何问题。我还添加了 Client 和 Client.Runtime 引用!我无法理解为什么它在控制台中有效,但在网站中无效。我是否缺少任何命名空间,参考?我已经提到了这些 URL集合尚未初始化、Sharepoint 字段尚未在 C# 中初始化、Sharepoint 客户端对象模型 属性或字段尚未初始化,但没有任何东西告诉我有关我的问题的任何信息。请帮忙。谢谢
根据我的测试,有时候我们可以忽略aspnet项目中的异常。(对于您的代码,您需要将 ["XXX"] 替换为 Client_XXX)
准备
通过 NuGet 包管理器安装Microsoft.SharePointOnline.CSOM到项目。
我的测试代码:首先是扩展方法(可选)
public static class CSOMExtensions
{
public static Task ExecuteQueryAsync(this ClientContext clientContext)
{
return Task.Factory.StartNew(() =>
{
clientContext.ExecuteQuery();
});
}
}
用我的替换你帖子中的一些代码:
List list = clientContext.Web.Lists.GetByTitle("CustomList1");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><RowLimit>1200</RowLimit></View>";
ListItemCollection collListItem = list.GetItems(camlQuery);
clientContext.Load(collListItem, li => li.Include(
pi => pi.Client_Title
//pi => pi["Application"],
// pi => pi["AIR_x0020_ID"]
));
//clientContext.ExecuteQueryAsync();
Task t2 = clientContext.ExecuteQueryAsync();
await t2.ContinueWith((t) =>
{
foreach (ListItem oListItem in collListItem)
{
System.Diagnostics.Debug.WriteLine(oListItem.Client_Title);
}
});
如果我们直接使用 ["Title"],它会在 aspnet 中崩溃,但 Client_XXX 不会站在我这边。
我没有深入研究为什么也没有尝试自定义字段。我会在有时间的时候研究更多
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句