SharePoint Get Attachment with Attachment Icon Using Javascript

How to get the attachment from a SharePoint list with an icon according to the type of attachment.

The following shows how to get the attachment from a SharePoint list with an icon according to the type of attachement.

Javascript Methods

var SiteURL = '';
function getAttachments(ID,ListName)
{
var PageURL = window.location.href;
var LastIndexOfPage = PageURL.substring(PageURL.lastIndexOf("/"));
PageURL += "/";
SplitLastIndexOfPage = LastIndexOfPage.split("/");
var FirstIndexOfPage = PageURL.split("/"+SplitLastIndexOfPage[1]+"/");
SiteURL = FirstIndexOfPage[0];
var lists = new SPAPI_Lists(SiteURL);
var items = lists.getAttachmentCollection(ListName,ID);
if (items.status == 200)
{
var rows = items.responseXML.getElementsByTagName('Attachment');
var str="";
for (var i=0; i<rows.length; i++)
{
temp=rows[i].childNodes[0].nodeValue;
var splitAttachmentURL = temp.split("/" +ID+"/");
if(splitAttachmentURL[1].length != 0)
{
var AttachmentExtn = getAttachmentIcon(splitAttachmentURL[1]);
str+='<a href="'+temp+'" target="_blank">'+AttachmentExtn+' </a>'+"<br /> ";
}
}
document.getElementById("att"+ID).innerHTML = str;
}
else
{
alert('There was an error: ' + items.statusText);
}
}

function getAttachmentIcon(fileName)
{
var fileExtension = fileName.substring(fileName.indexOf('.')+1).toLowerCase();
var imgTag;
if(fileExtension == 'xls' || fileExtension == 'xlsx')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/icxls.gif'> "+fileName;
}
else if(fileExtension == 'doc' || fileExtension == 'docx')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/icdoc.gif'> "+fileName;
}
else if(fileExtension == 'ppt' || fileExtension == 'pptx')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICPPT.gif'> "+fileName;
}
else if(fileExtension == 'txt')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ictxt.gif'>"+fileName;
}
else if(fileExtension == 'bmp')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICBMP.gif'> "+fileName;
}
else if(fileExtension == 'jpeg')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICJPEG.gif'> "+fileName;
}
else if(fileExtension == 'jpg')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICJPG.gif'>"+fileName;
}
else if(fileExtension == 'png')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICPNG.gif'>"+fileName;
}
else if(fileExtension == 'pdf')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='"+SiteURL+"/images/pdfico.png'>"+fileName;
}
else
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICGEN.gif'> "+fileName;
}
return imgTag;
}

Call Javascript in XSL as

<script type="text/javascript" language="javascript" >
getAttachments(<xsl:value-of select="@ID" />,'<xsl:value-of select="$ListName" />');
</script>

For latest SharePoint Updates and Solutions visit my blog @ http://stsadm2010.blogspot.com/

By Vivek Jagga   Popularity  (3177 Views)
Picture
Biography - Vivek Jagga
SharePoint Developer
For latest updates view My Blog