Skip to content

Instantly share code, notes, and snippets.

@anoopt
Last active June 30, 2023 14:12
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 anoopt/1c2387eeea1b49cb6de659ab1b2bf405 to your computer and use it in GitHub Desktop.
Save anoopt/1c2387eeea1b49cb6de659ab1b2bf405 to your computer and use it in GitHub Desktop.
NWF$(document).ready(function () {
$pnp.setup({
sp: {
headers: {
"Accept": "application/json; odata=verbose"
}
}
});
var isDisplayMode = document.location.pathname.indexOf("/DispForm.aspx") > -1;
if (isDisplayMode) {
showAttachments();
}
});
function showAttachments() {
/* get item ID from URL */
var urlParams = new URLSearchParams(window.location.search);
var itemId = urlParams.get('ID');
var item = $pnp.sp.web.lists.getByTitle("SimpleList").items.getById(itemId);
var attachmentsTarget = NWF$('#tbxAttachments');
item.attachmentFiles.select("FileName,ServerRelativeUrl").get().then(function (itemAttachments) {
var attachments = itemAttachments.map(function (result) {
return NWF$('<img/>', {
src: result.ServerRelativeUrl,
alt: result.FileName,
style: "margin:5px;width:100px;height:100px;float:left;",
class: "item-attachment"
});
});
/* add the images to the target element */
attachments.forEach(function (element) {
element.appendTo(attachmentsTarget);
});
/* add click event for each image */
NWF$(".item-attachment").each(function (index) {
NWF$(this).click(function () {
Swal.fire({
imageUrl: NWF$(this).attr('src'),
imageAlt: NWF$(this).attr('alt')
})
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment