Sharepoint - How to get file or list item icon in SharePoint 2013 using javascript
SharePoint has already lots of icons available in the DOCICON.xml file (14-Hive at C:/Program Files/Common Files/Microsoft Shared/web server extensions/14/TEMPLATE/XML)
Simplest way could be to load based on file extension "/_layouts/images/icXXX.gif (where icXXX, would be icdoc.gif, idpdf.gif, etc.)
You can use the script below :
<script type="text/javascript">
function getFileTypeIcon(fName, size) {
var dfd = $.Deferred();
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/maptoicon(filename='" + fName + "', progid='', size=" + size + ")",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
dfd.resolve(_spPageContextInfo.webAbsoluteUrl + "/_layouts/15/images/" + data.d.MapToIcon);
},
error: function (errMsg) {
if (errMsg.responseText) {
dfd.reject(errMsg.responseText);
}
}
});
return dfd.promise();
}
$(document).ready(function() {
// size = 0 for small and 1 for large
getFileTypeIcon('https://someurl/someName.pdf', 1).then(function (result) {
// Write the complete url to file icon
console.log(result);
});
});
</script>