Skip to content

Instantly share code, notes, and snippets.

@codegard1
Last active June 30, 2023 14:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codegard1/49dcf9beedfb43144686449b5b10b80e to your computer and use it in GitHub Desktop.
Save codegard1/49dcf9beedfb43144686449b5b10b80e to your computer and use it in GitHub Desktop.
Show current item's attachments on the form
/* In the context of a Nintex form in SharePoint:
/* use jQuery (NWF$) to fetch the attachments of the current list item and display them
/* on the form, using a DIV element inside a Multiline Text Control as the container. */
NWF.FormFiller.Events.RegisterAfterReady(function () {
/* CONFIGURATION */
var siteURL = "https://site.com"; // no trailing slash
var listName = "A List of Items with Image Attachments" // use the list display name;
var itemId = GetUrlKeyValue('ID'); // or ({ItemProperty:ID}) if you are embedding the code into the form
var url = siteURL + "/_api/web/lists/getByTitle('" + listName + "')/items(" + itemId + ")/AttachmentFiles";
var query = NWF$.ajax({
url: url,
type: 'GET',
headers: {
'accept': 'application/json; odata=verbose'
}
});
/* when the query returns data */
query.done(function (data) {
/* get the actual results from the data blob */
var results = data.d.results;
/* identify the element to hold the images */
var target = NWF$('#atch');
/* create an IMG element for each result,
i.e. each attachment URL */
var attachments = results.map(function (result) {
return NWF$('<img/>', {
src: result.ServerRelativeUrl,
alt: result.FileName,
/* style the images as thumbnails */
style: "margin:5px;width:100px;height:100px;float:left;"
});
});
/* add the images to the target element */
attachments.forEach(function (element) {
element.appendTo(target);
});
});
query.fail(function () {
console.log("Query Failed");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment