Skip to main content

How to get Document Type Icon using MapToIcon REST Endpoint

If you look at a Document Library View, you will also notice Document Type column which shows the icon corresponding to the Document Type. If it is a word document, it shows MS Word symbol as shown below.

Document Icon is highlighted
There could be scenarios where you would need to display the documents in a custom view and you would also want to make use of these icons. However this is not a very straightforward approach and this post will show you how to get that!

I was using REST API to get items from the library. So obviously I was looking at any column which would give me access to these icons. When I got into the columns in the response, the one which caught my attention was the field "DocIcon". But, this field contains only the extension of the document. For example, "docx", "html", "jpg" etc. So DocIcon was of no use as I wanted to get the image URL.

I also started looking at any endpoint which would get me this URL. During this search, I found MapToIcon endpoint (Reference: Webs REST API Reference). The REST call looks like this:

_api/web/maptoicon(filename='sampledocument.docx', progid='', size=0)

It takes three parameters. First parameter is filename, the second is Program ID (which I did not use and left it blank) and the last parameter is size of the icon. If Size = 0, it returns the 16x16 pixels image. If the Size = 1, it returns the 32x32 pixels image.

In the REST response, look at MapToIcon field. If Size is 0, the value will be "icdocx.png". If you set Size to 1, MapToIcon value will be "lg_icdocx.png".

A few points to be considered here:
  1. This endpoint is at web level (for obvious reason). However, we need to make this REST call for every type of document and store the value for all the items (of course, based on your programming logic).
  2. Do note that, it only returns the file name of the icon. It doesn't return the URL.

To resolve the second point, I looked at Developer Tools to understand where SharePoint has stored all these icons!

The images are stored under images folder under 15 hive. Example:

/_layouts/15/images/icdocx.png

Basically, when you get the response from MapToIcon endpoint, you need to prefix the above URL till "images/" to add a link to the images.

This was another learning for me and I thought I will share through a blog post. Let me know if you have any questions.

Comments

  1. Nice Post! There seems to be one issue with zip icon for 32*32 dimension. The REST API(_api/web/maptoicon(filename='sampledocument.zip', progid='', size=0)) is returning the file icon name for the query as lg_iczip.gif. But when we search for the file icon combining the layout image path(_layouts/15/images/lg_iczip.gif), it does not exist. Hope it may help someone.

    Regards
    Naresh Kanaujiya

    ReplyDelete
  2. Correcting the rest call with correct size parameter : _api/web/maptoicon(filename='sampledocument.zip', progid='', size=1

    ReplyDelete
  3. Thanks Naresh for the comments!

    ReplyDelete
  4. Hi, this api seems to be missing a lot of file types, like mp4 mp3.
    Looking at the modern library, it has icons for every file I could test with. It looks like the icons are cdn hosted rather than SP

    img class="FileTypeIcon-icon" title="mp4 File" src="https://spoprod-a.akamaihd.net/files/odsp-next-prod_ship-2017-09-01-sts_20170908.001/odsp-media/images/filetypes/16/video.png" style="width:16px;height:16px;" data-reactid=".0.0.0.0.0.1.0.0.0.0.$page-0.$dfb9408b-716a-4afd-8fd7-6a936098bc3c.0.0.2.$0.0.0"

    Any ideas is there is an updated API?

    ReplyDelete
    Replies
    1. @Dub Six, I dont have any updates on this. Will try to check. If I find any info, will update here.

      Delete

Post a Comment

Popular posts from this blog

Document ID Service - New Feature in SharePoint

When I explored this new feature of SharePoint in Online (this feature is available in SharePoint 2013 also), I found it interesting because it solved one of the common problems people faced in SharePoint. I will first explain about this feature and I'll cover the problem and the solution in another post. This feature allows Site Collection Admin assign a unique ID to every document in the site collection out of the box. If you recall, there was no out of the box way to assign a unique number to the documents across libraries in a site collection. Yes ID column is unique but only within a library. Document ID column contains a number which is unique across libraries. To start with, let's have a look at a typical document library. This is a familiar view for SP guys. By default, you don't get Document ID column in a library. All Items View To get Document ID column, you need to activate a site collection feature "Document ID Service" as shown below. S...

How to update Person field with multiple values using REST API

Person or Group field in SharePoint is similar to a Lookup field. When you are updating this field using REST API, you need to append "Id" to the name of the column in the body construct. For example, the body construct looks like this: data: { "__metadata": { "type": "SP.Data. ListName ListItem" }, "Title": "First Item", " PeopleField Id": "4" }; The highlighted portions should be replaced by the actual List Name and Column Name. In the above example, the REST call is updating a List item with Title and People columns. How to get the value for user ID ("4" in the above example) needs a separate explanation and that will be my next post! The above example works fine if Person field is configured to accept only one value. If we change the Person field to accept multiple values, how do we pass more than one value in the REST call? Since we normally separate user names with semicolon in peop...

Restrict Future or Past Date in List

Many a times you will come across a requirement wherein you need to restrict user from entering future or past date in a List or Document Library Date column. Developers are inclined towards writing either event handler or javascript to achieve this. But, hold on! You can achieve this without writing a single line of code. I have a list which stores Employee details like Employee Number, Name and Date of Joining. Here I need to enure that Date of Joining will not accept future date. Navigate to List Settings -> Validation Settings. Here you can enter the formula to restrict future date as shown below. If you change the logical operator, it will restrict past date. Also, you can put message which will be displayed to user. Now, when you try to enter future date and try Save, you will see the validation message. So, next time you have similar requirement, you know where to try first before jumping into coding!