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

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

All about SharePoint List View Styles

Sometimes, there are out of the box features which we tend to ignore and later when we do apply, we are more than happy about the feature which is readily available in SharePoint. One such feature is List View Style. I never thought I would write a post on this. However, whenever I spoke about this with users, people were excited to see the result. That prompted me to write this post. Instead of getting into only theory part, I will basically take use cases where these styles can be applied and also touch up on on some minor limitations with certain style. When you are creating/modifying a List view, you will get an option to select View Style. As shown below, there are 8 options available and Default is always set if you ignore this style. List of View Styles I will take typical Contact List and Announcement List to explian about these styles. Let us go one by one. Default: This view, as name suggest, is the default style in a view. This is one of the widely seen style

How to get SharePoint List or Library GUID via REST

Sometimes, you would need List or Library GUID to use that in some operation. In such cases, how do you get hold of GUID using REST API? There is a straight-forward end point which you can use: /_api/web/lists/getByTitle('ListTitle')/Id This will return the GUID of a List or Library. There is also another approach. However this approach works only if a List or Library contains at least one item. /_api/web/lists/getByTitle('ListTitle')/items This is a familiar end point which we use to fetch items of a List or Library. To get the GUID from the response, you would do: var listIDTemp = response.data.d.results[0].__metadata.id; var listID = listIDTemp.substring(listIDTemp.lastIndexOf("guid") + 5).split('\'')[0]; As you can see, we are doing string operations to fetch GUID from metadata.id. This contains information only if the response has at least one item.