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?

13,211

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

Share:
13,211
McAden
Author by

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, 2022

Comments

  • McAden
    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?

  • McAden
    McAden almost 13 years
    I had problems with the return. I was able to do it just fine in a for loop though. Everything else works great.
  • Zarepheth
    Zarepheth over 10 years
    This got me far enough to actually retrieve the files. Next step is to download from remote server and store on local server.