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. One of the lists I need to pull the d开发者_如何学编程ata for has attachments. I need to find a way to list those attachments but I can't seem to find a way to do so.
There's an "Attachments" field on the ListItem but it's only a boolean stating whether an attachment exists or not.
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?
I have also run into this problem and with the help of answer from ScottyG30 and answer on this thread I wrote a function for retrieving attachment from 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();
}
While this works for me, it doesn't seem to me as best solution when you need attachments (urls) for all items in large list (every item is 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));
});
精彩评论