Skip to main content

Get attachment with attachment icon

Recently I have a implemented a functionality in a SharePoint project, Get the attachment from a SharePoint list with a icon according to the type of attachement. After long practice we are able to resolve the same. Now I am sharing you the solution with you, I think it would help to you.

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]+"/");
//var lists = new SPAPI_Lists('http://sharepointdev:123/sites/myteamsite/phases/');
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>

Comments

Popular posts from this blog

GPT-5.2 Lands in Microsoft 365 Copilot: Smarter Reasoning for Workflows

 Today Microsoft announced that GPT-5.2 is now available in Microsoft 365 Copilot and Copilot Studio , bringing two tuned variants: GPT-5.2 Thinking for deep reasoning and strategic work, and GPT-5.2 Instant for fast writing, translation, and skill-building. In Copilot, the model connects to Work IQ to surface insights across your meetings, emails, and documents—helping you turn scattered information into actionable plans. If you use Copilot Chat or Copilot Studio, you can pick GPT-5.2 from the model selector and try prompts like asking for strategic takeaways from a meeting, building comparative tables of market leaders, or preparing focused talking points for upcoming conversations. Microsoft emphasizes model choice , so teams can select the model that best fits the task while keeping security, compliance, and privacy controls in place. Rollout starts today 11-Dec-2025 for users with a Microsoft 365 Copilot license and will expand over the coming weeks; Microsoft 365 Premium...

Why MFA Matters in Microsoft 365 (and How to Enforce It the Right Way)

If you’re still relying on passwords to protect Microsoft 365, you’re leaving a critical gap in your security. A single phished credential can be enough to access email, files, chats, and sensitive business data. Multi-Factor Authentication (MFA) adds a second verification step—such as an Authenticator prompt, a one-time code, or a security key—so a password alone can’t grant access. As a Microsoft 365 administrator and technology enthusiast, I’ve seen firsthand how implementing MFA dramatically reduces account takeover risk and strengthens organisational security. Who this is for This guide is aimed at Microsoft 365 administrators, IT teams, and business leaders looking for practical steps to bolster tenant security with consistent MFA enforcement. Whether you’re managing a small business or a larger enterprise, the principles here apply to everyone seeking to protect critical business data. The goal: require MFA for interactive sign-ins Enforcing MFA for all interactive use...

6 ways to check SharePoint Usage reports

Introduction SharePoint administrators always want to know which user is accessing any site or site collection extensively and which user has access to the site but not using it. The other site details required are related to traffic, access, edit, etc. The user's behavior changes as per the nature of the site and their working department like project team site, department site, and managerial workspace. Fortunately, Microsoft 365 has given multiple methods to generate or view various reports related to SharePoint usage. We will check all the methods one by one and see how they provide the required results. Several options are available at different parts in SharePoint Site or Admin Center where the Administrator can check status or create reports. Method-1. Check Site Usage in Site Contents Page. The Site contents page shows the analytics data like their unique visitors, total site visits, average time spent by the user, etc. The report can be generated for multiple days like 7, 3...