0

I am trying to get the file icons displayed in the standard view in the document libraries:

enter image description here

I want them to be displayed on a custom "file card" alongside the filename and a preview.

The SE article linked here didn't help as they suggest using the Fabric Icons. But I want the original SharePoint icons.

Edit: I added this code below to take care of different filetypes. Works like a charm.

"attributes": { "src": "='https://spoprod-a.akamaihd.net/files/fabric-cdn-prod_20201207.001//assets/item-types/20/' + 
if([$File_x0020_Type] == 'docx'|| [$File_x0020_Type] == 'dotx'  || 
[$File_x0020_Type] == 'pptx' || [$File_x0020_Type] == 'potx' || 
[$File_x0020_Type] == 'ppsx' || [$File_x0020_Type] == 'xlsx'  || 
[$File_x0020_Type] == 'pdf' || [$File_x0020_Type] == 'zip'||
 [$File_x0020_Type] == 'txt' || [$File_x0020_Type] == 'folder', [$File_x0020_Type], if([$File_x0020_Type] == 'mp4' || 
[$File_x0020_Type] == 'mpg' || [$File_x0020_Type] == 'avi', 'video', 
if([$File_x0020_Type] == 'jpg' || [$File_x0020_Type] == 'gif' || 
[$File_x0020_Type] == 'png' || [$File_x0020_Type] == 'png','photo', 
if([$File_x0020_Type] == 'json', 'code', if([$File_x0020_Type] == 'eml' || 
[$File_x0020_Type] == 'msg', 'email', 'genericfile'))))) + '.svg'"
1

You can create a img control to show the file type icon using JSON formatting.

You need to set the src property of img element based on [$File_x0020_Type]. In SharePoint online, default icon URLs are in below format:

Example: Word document Icon URL:

https://spoprod-a.akamaihd.net/files/fabric-cdn-prod_20201207.001//assets/item-types/20/docx.svg

You can get this URL by inspecting the icons in document library view:

enter image description here

Similarly you can get the URLs for other file type icons & build JSON like:

{
   "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
   "elmType": "img",
   "attributes": {
      "src": "=if([$File_x0020_Type] == 'png', 'https://spoprod-a.akamaihd.net/files/fabric-cdn-prod_20201207.001//assets/item-types/20/photo.svg', if([$File_x0020_Type] == 'docx','https://spoprod-a.akamaihd.net/files/fabric-cdn-prod_20201207.001//assets/item-types/20/docx.svg', ''))",
      "alt": "=[$File_x0020_Type]"
   },
   "style": {
      "width": "50%",
      "max-width": "50%"
   }
}

Sample output:

enter image description here

This is just an example, you can modify it based on the different file types (by adding more conditions in src attribute) & utilize it in view formatting JSON.

Update: Simplified version of above JSON:

{
   "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
   "elmType": "img",
   "attributes": {
      "src": "= 'https://spoprod-a.akamaihd.net/files/fabric-cdn-prod_20201207.001//assets/item-types/20/' + if([$File_x0020_Type] == 'png', 'photo.svg', if([$File_x0020_Type] == 'docx', 'docx.svg', ''))",
      "alt": "=[$File_x0020_Type]"
   },
   "style": {
      "width": "50%",
      "max-width": "50%"
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.