单击按钮时 asp.net 中的 Sharepoint 集合尚未初始化 - C# asp.net
Sharepoint collection has not been initialized in asp.net on button click - C# asp.net
我是 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) 处给我错误:
An exception of type 'Microsoft.SharePoint.Client.CollectionNotInitializedException'
occurred in Microsoft.SharePoint.Client.Runtime.dll but was not
handled in user code
Additional information: The collection has not been initialized. It
has not been requested or the request has not been executed. It may
need to be explicitly requested.
这里是详细的错误:
Microsoft.SharePoint.Client.CollectionNotInitializedException was
unhandled by user code HResult=-2146233079 Message=The collection
has not been initialized. It has not been requested or the request has
not been executed. It may need to be explicitly requested.
Source=Microsoft.SharePoint.Client.Runtime StackTrace:
at Microsoft.SharePoint.Client.ClientObjectCollection`1.d__0.MoveNext()
at 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)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
请注意,我在控制台应用程序中没有任何问题,但 asp.net 按钮单击操作。我也添加了客户端和 Client.Runtime 引用!我无法理解为什么它在控制台中有效但在网站中无效。我是否缺少任何名称空间,参考?我已经引用了这些 URL , Sharepoint field has not been initialized in C#, ,但没有任何关于我的问题的信息。请帮忙。谢谢
根据我的测试,有些时候我们可以忽略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();
});
}
}
用我的 post 替换一些代码:
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 不会在我这边。
我没有深入研究原因,也没有尝试自定义字段。有时间我再研究一下
我是 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) 处给我错误:
An exception of type 'Microsoft.SharePoint.Client.CollectionNotInitializedException' occurred in Microsoft.SharePoint.Client.Runtime.dll but was not handled in user code
Additional information: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
这里是详细的错误:
Microsoft.SharePoint.Client.CollectionNotInitializedException was unhandled by user code HResult=-2146233079 Message=The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
Source=Microsoft.SharePoint.Client.Runtime StackTrace: at Microsoft.SharePoint.Client.ClientObjectCollection`1.d__0.MoveNext() at 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) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
请注意,我在控制台应用程序中没有任何问题,但 asp.net 按钮单击操作。我也添加了客户端和 Client.Runtime 引用!我无法理解为什么它在控制台中有效但在网站中无效。我是否缺少任何名称空间,参考?我已经引用了这些 URL
根据我的测试,有些时候我们可以忽略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();
});
}
}
用我的 post 替换一些代码:
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 不会在我这边。
我没有深入研究原因,也没有尝试自定义字段。有时间我再研究一下