【问题标题】:How to get AttachmentFiles when calling GetItemById using Sharepoint CSOM?使用 Sharepoint CSOM 调用 GetItemById 时如何获取 AttachmentFiles?
【发布时间】:2017-02-02 04:00:34
【问题描述】:

我想花点时间记录一个问题,当我尝试使用 GetItemById 获取 AttachmentFiles 时解决了一个问题,该问题使用了用于 Sharepoint 的 .NET 客户端对象模型 (CSOM),特别是 Microsoft.SharePoint.Client。我无法找到这个问题的明确答案。以下是获取 Sharepoint 列表项的基本方法,您可以在 MSDN 任何其他网站上找到如何操作:

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);

我现在将在下面的答案中发布如何正确包含附件:

【问题讨论】:

    标签: sharepoint csom


    【解决方案1】:

    这是正确的做法:

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 2021-07-05
      • 2018-01-03
      相关资源
      最近更新 更多