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)
Step 2
Upload some dummy document with a different file type.
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).
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.
- {
- "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
- "elmType": "img",
- "attributes": {
- "src": "@thumbnail.medium"
- },
- "style": {
- "display": "block",
- "margin": "0 auto",
- "max-height": "42px"
- }
- }
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.
- {
- "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
- "elmType": "div",
- "attributes": {
- "title": "=if([$ContentType] == 'Folder','','Hey')"
- },
- "children":[{
- "elmType": "img",
- "attributes": {
- "src": "@thumbnail.medium"
- },
- "style": {
- "display": "block",
- "margin": "0 auto",
- "max-height": "42px"
- }
-
- }]}
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.