0

I have an SPList. How I can get attachment URLs for every item in the list?

1

2 Answers 2

1

Keep in mind that bit of code might be slow depending on the size of the list:

foreach (SPListItem listItem in list.Items)
{
    var attachmentURLs = GetAttachmentUrls(listItem)
}

private IEnumerable<string> GetAttachmentUrls(SPListItem item)
{
  return from string fileName in item.Attachments
         orderby fileName
         select SPUrlUtility.CombineUrl(item.Attachments.UrlPrefix, fileName);
}

Credit to this post: How do I get the url for attachments on an SPListItem using C#? I just added a foreach loop.

4
  • 1
    The question is tagged SharePoint Online, if that is the truth this code will not work Commented Mar 16, 2016 at 13:50
  • Just noticed that, let's see if the user confirms it. Thanks. Commented Mar 16, 2016 at 13:56
  • I do in foreach(DataRow row in table.Rows) like u say with SPListItem, but I see System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.String,System.String] when out this. Commented Mar 16, 2016 at 14:19
  • If you marked it as an answer, I assume this was SharePoint Server and not Office 365? Commented Mar 16, 2016 at 16:16
1

You can get list item attachments using REST api as in below code:

var url = url + "/_api/web/lists/getbytitle('ListTitle')/items(1)/AttachmentFiles";

$.ajax({
    url: url,
    accepts: {
        json: "application/json;odata=verbose"
    },
    method: "GET",
    success: onQuerySuccess,
    error: onQueryError
});

function onQuerySuccess(data) {
    if (data) {
        $.each(data.d.results, function () {
            // do something
            this.ServerRelativeUrl;
        });
    }
}

Here "items(1)" is the id of the list item

1
  • I must do it task without js, but thanks for asnwer. Commented Mar 16, 2016 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.