Suppose you want a visual display of the images that you attach to a SharePoint list. Clicking on each attachment to open it can be a hassle instead of just having it automatically display the images on load.
By default, the attachments are listed as links:
If you view the properties of a SharePoint list item, you will see that the link to each attachment is calculated by:
[Site Url]/Attachments/[Item ID]/[Attachment Name]
So, if we can grab the item id of the list item, and then determine the attachment names, we will be able to use those properties to display the image with an img tag.
First thing's first. If you view the source of that default SharePoint item view page, you will see that the attachments are store din the element: idAttachmentsTable. We want to grab all of the attachment names in that element. To do so, we will iterate through the span elements and parse out the attachments:
Now that we have the names of the attachments, we want to grab the current item id of our list item, which we can simply grab from the source url.
Call to get the item ID:
Function to grab the parameter from the source url:
\\\[").replace(/[\]]/, "\\\]"); By default, the attachments are listed as links:
If you view the properties of a SharePoint list item, you will see that the link to each attachment is calculated by:
[Site Url]/Attachments/[Item ID]/[Attachment Name]
So, if we can grab the item id of the list item, and then determine the attachment names, we will be able to use those properties to display the image with an img tag.
First thing's first. If you view the source of that default SharePoint item view page, you will see that the attachments are store din the element: idAttachmentsTable. We want to grab all of the attachment names in that element. To do so, we will iterate through the span elements and parse out the attachments:
<script type="text/javascript"> spanTag = document.getElementById("idAttachmentsTable").getElementsByTagName("span"); var attachmentArray = new Array(); for (var i = 0; i < spanTag.length; i++) { filename = spanTag[i].innerHTML; var index = filename.indexOf('>')+1; var lastindex = filename.lastIndexOf('<'); var name = filename.substring(index, lastindex); name = name.replace(/ /g, '%20'); name = name.replace(/'/g, '%27'); for( var x = 0; x < imgExtensions.length; x++ ) { if( name.indexOf(imgExtensions[x]) >=0 ) { attachmentArray.push([name]); break; } } } </script>
Now that we have the names of the attachments, we want to grab the current item id of our list item, which we can simply grab from the source url.
Call to get the item ID:
var paramID = getParameterByName("ID");
Function to grab the parameter from the source url:
function getParameterByName(name) { name = name.replace(/[\[]/, "
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
Now, we will use the paramID and the attachment names to create our img tags and append it to the Attachments element:
var ctrl = document.getElementById("idAttachmentsTable"); var attachmentString = ""; for( i = 0; i < attachmentArray.length; i++) { attachmentString += " <img src='" + listAttachmentUrl + paramID + "/"+ attachmentArray[i] + "' width='400px' border='2'>"; } ctrl.parentNode.innerHTML += attachmentString;
After adding our script:
To use this script, you will need to use SharePoint designer to edit the "View" page of the list. Navigate to the end of the PlaceHolderMain content placeholder, and right before the closing "</asp:Content>" tag, place the script below in it's entirety. Make sure to modify the listAttachmentUrl to reflect your site and list names:
<script language="javascript" type="text/javascript"> var listAttachmentUrl = '/mysite/Lists/mylist/Attachments/'; var imgExtensions = new Array(".jpg",".jpeg", ".png", ".bmp", ".tif", ".tiff"); LoadAllAttachments(); function LoadAllAttachments() { spanTag = document.getElementById("idAttachmentsTable").getElementsByTagName("span"); var attachmentArray = new Array(); for (var i = 0; i < spanTag.length; i++) { filename = spanTag[i].innerHTML; var index = filename.indexOf('>')+1; var lastindex = filename.lastIndexOf('<'); var name = filename.substring(index, lastindex); name = name.replace(/ /g, '%20'); name = name.replace(/'/g, '%27'); for( var x = 0; x < imgExtensions.length; x++ ) { if( name.indexOf(imgExtensions[x]) >=0 ) { attachmentArray.push([name]); break; } } } var paramID = getParameterByName("ID"); var ctrl = document.getElementById("idAttachmentsTable"); var attachmentString = ""; for( i = 0; i < attachmentArray.length; i++) { attachmentString += " <img src='" + listAttachmentUrl + paramID + "/"+ attachmentArray[i] + "' width='400px' border='2'>"; } ctrl.parentNode.innerHTML += attachmentString; } function getParameterByName(name) { name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); var regexS = "[\\?&]" + name + "=([^&#]*)"; var regex = new RegExp(regexS); var results = regex.exec(window.location.href); if(results == null) return ""; else return decodeURIComponent(results[1].replace(/\+/g, " "));} </script>