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.
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:
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:
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:
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.
Document Icon is highlighted |
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:
- 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).
- 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.
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.
ReplyDeleteRegards
Naresh Kanaujiya
Correcting the rest call with correct size parameter : _api/web/maptoicon(filename='sampledocument.zip', progid='', size=1
ReplyDeleteThanks Naresh for the comments!
ReplyDeleteHi, this api seems to be missing a lot of file types, like mp4 mp3.
ReplyDeleteLooking 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?
@Dub Six, I dont have any updates on this. Will try to check. If I find any info, will update here.
Delete