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 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
).
McAden
Software developer for 12+ years primarily on full-stack web projects. Self-published 2 mobile games using Unity3d. Well-versed in a variety of technologies and languages but working primarily in C# and TypeScript. BA in Comp Sci from Harding University.
Updated on June 05, 2022Comments
-
McAden about 2 years
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 data 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?
-
Venkat Vangalapudi almost 13 yearsPlease look in to the below blog post, http://vangalvenkat.blogspot.com/2011/08/sharepoint-2010-getting-list-item.html
-
-
McAden almost 13 yearsI had problems with the return. I was able to do it just fine in a for loop though. Everything else works great.
-
Zarepheth over 10 yearsThis got me far enough to actually retrieve the files. Next step is to download from remote server and store on local server.