Thumbnail Or Preview Of Files In SharePoint Document Library Using Column Formatting

In this article, you will learn about thumbnail or preview of files in the SharePoint document library using column formatting.

Background

 
I have seen people requesting this feature in the past where we need to display file preview thumbnail for a document in the SharePoint library.  We are getting some awesome features in SharePoint Online nowadays, and very frequently as well. In this article, we will learn about how to display thumbnail or preview for any file type in SharePoint document library using the column formatting feature. This article is part of a series to learn and know the howabout of SharePoint column formatting.
 
First, let us set up our Document library with the required column. 
 
Step 1 
 
Create a column with name 'Preview' in any of Document Library.
 
Column Type - Calculated column
 
Enter Formula as  =""  (just empty string using double quotation mark)
 
Thumbnail Or Preview Of Files In SharePoint Document Library Using Column Formatting
 
Step 2
 
Upload some dummy document with a different file type. 
 
Thumbnail Or Preview Of Files In SharePoint Document Library Using Column Formatting
 
Step 3 
 
Now, let us format this column. We need to Edit the column from the Document Library settings.
 
An alternate and fast way is to use modern UI experience to format this column (the below screenshot depicts the same).
 
Thumbnail Or Preview Of Files In SharePoint Document Library Using Column Formatting
 
Step 4 - Add below JSON.
 
Here, we are creating an image DOM element and using @thumbnail.medium as src. @thumbnail is the default property which is being used here for file preview. 
  1. {    
  2.     "$schema""https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",    
  3.     "elmType""img",    
  4.     "attributes": {    
  5.         "src""@thumbnail.medium"    
  6.     },    
  7.     "style": {    
  8.         "display""block",    
  9.         "margin""0 auto",    
  10.         "max-height""42px"    
  11.     }    
  12. }    
Thumbnail Or Preview Of Files In SharePoint Document Library Using Column Formatting
 
So based on file type it will create file preview automatically and displays in view. Please note that pdf, docx, pptx and around other 170 file types are supported (ref - video). 
 
This concludes our learning on how to use @thumbnail with column formatting for file preview in list view.
 
Now, let us see what different attributes are available with @thumbnail which can be used. Depending on how you want to format your column you can use a different pattern too.
 
pattern is @thumbnail.property
 
 @thumbnail.small  auto small sized thumbnail
 @thumbnail.medium  auto medium sized thumbnail
 @thumbnail.large     auto large sized thumbnail
 @thumbnail.200  custom - 200 width and 200 height
  @thumbnail.200x100  custom - 200 width and 100 height 
 
A more advanced example is below, here we are adding title attribute so that on hover of preview image we get some tooltip. It also handles a scenario where if the content type is a folder, it will set the title attribute to empty string but if it is anything else, it will be a simple 'Hey' message.
  1. {  
  2.     "$schema""https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",  
  3.     "elmType""div",  
  4.     "attributes": {  
  5.         "title""=if([$ContentType] == 'Folder','','Hey')"  
  6.     },  
  7.     "children":[{  
  8.     "elmType""img",  
  9.     "attributes": {  
  10.         "src""@thumbnail.medium"  
  11.     },  
  12.     "style": {  
  13.         "display""block",  
  14.         "margin""0 auto",  
  15.         "max-height""42px"  
  16.     }  
  17.       
  18. }]}  
I had been exploring column formatting and it can do awesome things. Before this feature if we had to format our list view we had to use link or use custom code to format data based on our requirement. 
 
That's it for now. Thanks for reading.
 
This article is inspired by the contribution of @theChrisKent at the link
 
P.S - I did not find a list of supported file formats to preview. If anyone has any reference to the list of supported file formats in column formatting, please share in the comments section.