![](/img/trans.png)
[英]How to retrieve list item attachments with SharePoint 2013 Event Receiver in correct order
[英]How can I retrieve a list item's attachments, or at least the paths to the attachments from SharePoint via the Silverlight Client Object Model?
I've got a SharePoint site that I'm putting a Silverlight frontend on using the Silverlight Client Object Model. 我需要提取數據的列表之一有附件。 我需要找到一種方法來列出這些附件,但我似乎找不到這樣做的方法。
ListItem 上有一個“附件”字段,但它只是一個 boolean 說明附件是否存在。
I've seen plenty of examples of this using SPListItem, but how would I go about doing this using the Silverlight Client Object Model instead?
我也遇到了這個問題,在 ScottyG30 的回答和在這個線程上的回答的幫助下,我寫了一個 function 來從 ListItem 中檢索附件:
// this method needs to be executed in background thread
public String[] GetAttachments(ClientContext ctx, List list, ListItem item)
{
// these properties can be loaded in advance, outside of this method
ctx.Load(list, l => l.RootFolder.ServerRelativeUrl);
ctx.Load(ctx.Site, s=>s.Url);
ctx.ExecuteQuery();
// get the item's attachments folder
Folder attFolder = ctx.Web.GetFolderByServerRelativeUrl( list.RootFolder.ServerRelativeUrl + "/Attachments/" + item.Id);
FileCollection files = attFolder.Files;
// I needed only urls, so I am loading just them
ctx.Load(files, fs => fs.Include(f => f.ServerRelativeUrl));
ctx.ExecuteQuery();
// now you have collection of files
return (from file in files select ctx.Site.Url + file.ServerRelativeUrl).ToArray();
}
雖然這對我有用,但當您需要大型列表中所有項目的附件(url)時(每個項目都在executing query
),它似乎不是最佳解決方案。
ClientContext spContext = ClientContext.Current;
File.OpenBinaryDirect(spContext, spContext.Web.ServerRelativeUrl + "/lists/[ListName]/Attachments/[ItemID]/[File Name]", (w, f) =>
{
var foo = f.Stream;
}, (q, w) => {
handler(this, new Exception(w.Message));
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.