0

Working on a specific list that contains images as attatchments. How could I display them as image in infopath or list it self? Currently getting it as a link. I need it to be as a image for report purposes

0

By using below steps you can set image in your out of the box List.

1.Create "Picture" type Column in your list.

2.Create one Event Handler with ItemAttachmentAdded event like below:

public override void ItemAttachmentAdded(SPItemEventProperties properties)

       {
           base.ItemAttachmentAdded(properties);

           SPListItem item = properties.ListItem;
           //SPAttachmentCollection attach = item.Attachments;
           string attachmentUrl = item.Attachments.UrlPrefix + item.Attachments[0];

           item["PictureFieldName"] = attachmentUrl;

           item.Update();
      }

Now, just add item with attachment and you will see attached image in Picture Column field.

similar issue post for your reference:

https://social.technet.microsoft.com/Forums/office/en-US/2cb2f1d0-f8d8-4c7d-8ae0-83cbc3e81b0c/display-attached-image-as-picture?forum=sharepointgeneral

|improve this answer|||||
  • Yes, that is my post. Creating a multiple line collum helped me with inserting the picture. Would it be possible to do it automatically when attachment was added? – Lukas Šliužas Jan 31 '19 at 6:43
  • Would it be showed as image? – Lukas Šliužas Jan 31 '19 at 8:19
  • 1
    It cannot be automatically displayed as an image when attachment was added. – Lisa Chen MSFT Jan 31 '19 at 8:40
0

I managed to add this code as a content editor script in the "Default Display Form" of a list item. It's not a very clean solution, however it shows the attached pictures (all of them) directly in the default display form.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>

<script type="text/javascript"> 
$(function() {
  var itemId = getQueryString()["ID"];
  var listName = "Example List";
  var baseURL = "https://www.Example.com"
  var attachmentlink = getAttachments(baseURL, listName, itemId);
  var attachmentnumbers = getNumAttachments(baseURL, listName, itemId);

  // Find the form's main table
  var table = $('table.ms-formtable');

  // Add a row with the Queried Picture Entry for each attached picture
  for (var i = 0; i < attachmentnumbers; i++) {
    if (checkURL(attachmentlink[i])) {
        table.append("<tr><td class='ms-formlabel'><h3 class='ms-standardheader'>Attached Picture</h3></td>" +
                   "<td><a href='" + attachmentlink[i] + "'><img src='" + attachmentlink[i] + "' width=400></img></th></tr>");
    }
  }
})

function getAttachments(baseURL,listName,itemId) {

    var requestUri = baseURL + "/team/wmspr/X360/_api/web/lists/getbytitle('" + listName + "')/items('" + itemId + "')/AttachmentFiles";
    var str_array = [];
    $.ajax({
        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_array.push(baseURL + data.d.results[i].ServerRelativeUrl);
            }
        },
        error: function (err) {
        }
    });
    return str_array;
}

function getNumAttachments(baseURL,listName,itemId) {

    var requestUri = baseURL + "/team/wmspr/X360/_api/web/lists/getbytitle('" + listName + "')/items('" + itemId + "')/AttachmentFiles";
    var str = "";
    $.ajax({
        url: requestUri,
        type: "GET",
        headers: { "ACCEPT": "application/json;odata=verbose" },
        async: false,
        success: function (data) {
            str = data.d.results.length;
        },
        error: function (err) {
        }
    });
    return str;
}

function checkURL(url) {
    return(url.match(/\.(jpeg|jpg|gif|png)$/) != null);
}

function getQueryString() {
  var assoc = new Array();
  var queryString = unescape(location.search.substring(1));
  var keyValues = queryString.split('&');
  for (var i in keyValues) {
    var key = keyValues[i].split('=');
    assoc[key[0]] = key[1];
    }
  return assoc;
}
</script>

suggestions for a proper clean up are appreciated.

|improve this answer|||||
New contributor
me_ts is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.