6

I have a requirement from a user that when they submit the spec of a laptop or printer to a SharePoint list that the details are displayed on a separate page. I'm working on getting the text details to show, but part of the requirements is that the user can upload a picture of the kit which would then display.

I thought using an InfoPath form then creating a data view to the list, but that only shows the file name and not the actual image. Any ideas?

Dazza
  • 767
  • 1
  • 11
  • 27

4 Answers4

9

You can do couple of steps

  1. Get list attchment using following rest query

    siteurl/_api/web/Lists/getByTitle('list tiel')/Items(listitemid)/AttachmentFiles

  2. get the attachment file path from the results. Example

    var imgPath = data.d.results[0].ServerRelativeUrl

  3. Bind the file path to the html img control

    <div><img id="imgProduct" /></div>

    $("#imgProduct").attr(src, imgPath);

Venkat Konjeti
  • 4,959
  • 1
  • 10
  • 19
3

The REST solution is probably the most elegant. But an alternative is to write a script and use a content editor webpart to attach a script to solve the issue by client side rendering. An example is shown below (I modified some things from an older script I had and have not tested it, so I am pretty sure you will need to do some testing and tweaking to make it work):

_spBodyOnLoadFunctionNames.push("imgshow");

// Function Name:       getElementsByContains
// Function Purpose:    Returns array of all DOM elements with any value in the array passed into within the elements of a certain tag

function getElementsByContains(val, tag) {
    var i;
    const getElementsByContains = [];
    const allElements = document.getElementsByTagName(tag);
    for (let key in allElements) {
        if (isNumeric(key)) {
            if(allElements.hasOwnProperty(key)) {
                const element = allElements[key];
                if(element.innerHTML != undefined) {
                    if(Array.isArray(val)) {
                        for(i = 0; i < val.length; i++) {
                            if(element.innerHTML.search(val[i]) != -1) {
                                getElementsByContains.push(element);
                                break;
                            }
                        }
                    } else {
                        if(element.innerHTML.search(val) != -1) {
                            getElementsByContains.push(element);
                        }
                    }
                }        
            }
        }
    }
    return getElementsByContains;
}

// Function Name:       imgshow
// Function Purpose:    Show imgs displayed as text

function imgshow() {
    var planLink;
    var arrCalCol = getElementsByContains(["&lt;img", "<img"], "td"); //Can be td or anything, look in your DOM
    debugger;
    for (i=0; i < arrCalCol.length; i++) {
        if (arrCalCol[i].innerHTML.search("&lt;img") != -1 || arrCalCol[i].innerHTML.search("<img") != -1) {
            var planLink = arrCalCol[i];
            var pic = document.createElement("img");

            pic.src = "____________" //Add what you need to extract the attributes from the img string including the src field, then dump your tag attributes here other than src and etc. before the element gets amended/appended.
            pic.style = "height:35px; width:35px";

            planLink.innerHTML = "";

            if (planLink) {
                planLink.appendChild(pic);
            }
        }
    }
}
Isa
  • 410
  • 4
  • 14
2

Create one calculated field and assign your image URL with the following equation in calculated field,

="<div><img src=[URLField or URL]</div>"

for more details visit this and this

SynozeN Technologies
  • 1,199
  • 12
  • 25
1

If the image is uploaded to a seperate library and lookup column can be used to connect the image to the kit. On the seperate page you can use the content query webpart with the [PageQueryString] attribute within the filter of it. Then all your information can be showed on one page with help of the content query webpart. Or you can even redesign the dispform page of the kit list and add the content query webpart there with a reference of [pagefieldvalue:id] connected to the lookup column of the image.

bonm014
  • 66
  • 6