Question

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

Était-ce utile?

La solution

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.

Autres conseils

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top