Getting the default document icon using Office 365 SharePoint REST API

If you assumed that SharePoint’s REST API would return the document icon for your document by default, you’d be wrong. If you don’t assume that, then you’re smarter than me. Come to find out there is a separate API to call to get the icon.

The REST API to get the icon is

https://%5Btenant%5D.sharepoint.com/_api/web/maptoicon(filename='filename.pdf', progid='', size='0')

The size options, 1 or 0, return the image for 32×32 or 16×16, respectively. If you’re listing your documents in a list view similar to SharePoint, you’ll want to use 0.

Here’s a little snippet using it with jQuery to get your icons back:

//fileName would be any filename, like This is my file.pdf or Document.docx
var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/maptoicon(filename='" + fileName + "',progid='',size=0)";
$.getJSON(url)
 .then((data) => {
 var icon = $("<img src='" + _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/images/" + data.value + "'/>")
 //now throw your icon object into wherever you want to use it
 })

Til next time, Happy SharePointing!

Advertisements

One thought on “Getting the default document icon using Office 365 SharePoint REST API

  1. Pingback: Build or Buy Your SharePoint Intranet?; Mobile Device Compliance; SharePoint Roadmap Coming Soon

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s