I have a custom view form for my list. The form needs to display the contents of the list item as well as the photo that is attached to the list item. Currently I am having the end user copy and paste the filename into a separate column and using that to complete the src for the <img> tag.

I have used the following code to display the image on the .aspx form:

<img border="0" src="/Lists/Barring Records/Attachments/{@ID}/{@photo}" style="max-width: 1000px; max-height: 600px;"/>

where @ID is the list item ID and @photo is the copy-and-paste filename. This works but is awful for the end user.

Is there a way to find and reference the first attachment for the list item and insert it where the {@photo} goes? I do not care about any other attachments but the first. I would prefer to do this on the form page, but can put the code elsewhere if instructed.

I have SharePoint Foundations 2010


you can grab the attatchment name with this function:

function getAttachment(itemId, listName) {

  var url = _spPageContextInfo.webAbsoluteUrl;
  var requestUri = url + "/_api/web/lists/getbytitle('" + listName + "')/items(" +   itemId + ")/AttachmentFiles";
  var str = "";

   // execute AJAX request
      url: requestUri,
      type: "GET",
      headers: { "ACCEPT": "application/json;odata=verbose" },
      async: false,
      success: function (data) {
         for (var i = 0; i < data.d.results.length; i++) {
             str = data.d.results[i].ServerRelativeUrl;
     error: function (err) {

@Colbs, your code should work in sp13, however, for sp10, it is not the answer.

@Ryan, in my opinion, you might prefer a codeless solution, right? you can use asset library, for end users, they create folder instead of list item, the pictures uploaded in a folder will display in thumbnail view, users can easily preview them when open a folder in this library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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