如何在使用Sharepoint CSOM调用GetItemById时获取AttachmentFiles?
我想花一点时间来记录一个问题,我在尝试获取AttachmentFiles时解决了这个问题,同时使用用于Sharepoint的.NET客户端对象模型(CSOM),特别是Microsoft.Sharepoint.Client,使用GetItemById。我找不到这个问题的明确答案。以下是获取Sharepoint列表项的基本方法,您可以在MSDN任何其他网站上找到该方法:如何在使用Sharepoint CSOM调用GetItemById时获取AttachmentFiles?,sharepoint,csom,Sharepoint,Csom,我想花一点时间来记录一个问题,我在尝试获取AttachmentFiles时解决了这个问题,同时使用用于Sharepoint的.NET客户端对象模型(CSOM),特别是Microsoft.Sharepoint.Client,使用GetItemById。我找不到这个问题的明确答案。以下是获取Sharepoint列表项的基本方法,您可以在MSDN任何其他网站上找到该方法: var siteUrl = "http://MyServer/sites/MySiteCollection"; var clie
var siteUrl = "http://MyServer/sites/MySiteCollection";
var clientContext = new ClientContext(siteUrl);
var site = clientContext.Web;
var targetList = site.Lists.GetByTitle("Announcements");
var targetListItem = targetList.GetItemById(4);
clientContext.Load(targetListItem, item => item["Title"]);
clientContext.ExecuteQuery();
Console.WriteLine("Retrieved item is: {0}", targetListItem["Title"]);
// This will throw an AttachmentFiles "Not Initialized" Error
Console.WriteLine("AttachmentFiles count is: {0}", targetListItem.AttachmentFiles.Count);
我现在将发布如何在下面的答案中正确包含附件:以下是正确的方法:
var siteUrl = "http://MyServer/sites/MySiteCollection";
var clientContext = new ClientContext(siteUrl);
var site = clientContext.Web;
var targetList = site.Lists.GetByTitle("Announcements");
var targetListItem = targetList.GetItemById(4);
var attachments = targetListItem.AttachmentFiles;
clientContext.Load(targetListItem, item => item["Title"]);
clientContext.Load(attachments)
clientContext.ExecuteQuery();
Console.WriteLine("Retrieved item is: {0}", targetListItem["Title"]);
// This will no longer throw the error
Console.WriteLine("AttachmentFiles count is: {0}", targetListItem.AttachmentFiles.Count);
以下是正确的方法:
var siteUrl = "http://MyServer/sites/MySiteCollection";
var clientContext = new ClientContext(siteUrl);
var site = clientContext.Web;
var targetList = site.Lists.GetByTitle("Announcements");
var targetListItem = targetList.GetItemById(4);
var attachments = targetListItem.AttachmentFiles;
clientContext.Load(targetListItem, item => item["Title"]);
clientContext.Load(attachments)
clientContext.ExecuteQuery();
Console.WriteLine("Retrieved item is: {0}", targetListItem["Title"]);
// This will no longer throw the error
Console.WriteLine("AttachmentFiles count is: {0}", targetListItem.AttachmentFiles.Count);