"Object reference not set to an instance of an object on server. The object is associated with property File" 从 SharePoint 读取文件时
"Object reference not set to an instance of an object on server. The object is associated with property File" when read file from SharePoint
我已经从 url 共享点读取了文件,它 returns 那个错误:
Object reference not set to an instance of an object on server. The
object is associated with property File
我试图计算这个文件夹中的文件数量 url 所以它可以算作是真的,但是当我打印文件名时,它在行 this.lblMessage.Text = listItems.First().File.Name.ToString();
这是我的代码:
var targetSiteURL = new Uri("https://mycompany.sharepoint.com/sites/test");
ClientContext cxt = new ClientContext(targetSiteURL.GetLeftPart(UriPartial.Authority));
cxt.Credentials = new SharePointOnlineCredentials(login, securePassword);
var list = cxt.Web.GetList("/sites/test/Shared%20Documents");
var listItems = list.GetItems(new CamlQuery());
cxt.Load(listItems,
items => items.Include(
item => item.File));
cxt.ExecuteQuery();
this.lblMessage.Text = listItems.First().File.Name.ToString();
使用 Caml 查询仅过滤库中的文件,因为 item.File.Name 用于文件而不是文件夹:
var list = cxt.Web.GetList("/sites/test/Shared%20Documents");
Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<View Scope='RecursiveAll'>
<Query>
<Where><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq></Where>
</Query>
</View>";
var listItems = list.GetItems(camlQuery);
cxt.Load(listItems,items => items.Include(item => item.File));
cxt.ExecuteQuery();
Console.WriteLine(listItems.First().File.Name);
我已经从 url 共享点读取了文件,它 returns 那个错误:
Object reference not set to an instance of an object on server. The object is associated with property File
我试图计算这个文件夹中的文件数量 url 所以它可以算作是真的,但是当我打印文件名时,它在行 this.lblMessage.Text = listItems.First().File.Name.ToString();
这是我的代码:
var targetSiteURL = new Uri("https://mycompany.sharepoint.com/sites/test");
ClientContext cxt = new ClientContext(targetSiteURL.GetLeftPart(UriPartial.Authority));
cxt.Credentials = new SharePointOnlineCredentials(login, securePassword);
var list = cxt.Web.GetList("/sites/test/Shared%20Documents");
var listItems = list.GetItems(new CamlQuery());
cxt.Load(listItems,
items => items.Include(
item => item.File));
cxt.ExecuteQuery();
this.lblMessage.Text = listItems.First().File.Name.ToString();
使用 Caml 查询仅过滤库中的文件,因为 item.File.Name 用于文件而不是文件夹:
var list = cxt.Web.GetList("/sites/test/Shared%20Documents");
Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<View Scope='RecursiveAll'>
<Query>
<Where><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq></Where>
</Query>
</View>";
var listItems = list.GetItems(camlQuery);
cxt.Load(listItems,items => items.Include(item => item.File));
cxt.ExecuteQuery();
Console.WriteLine(listItems.First().File.Name);