SharePoint Online: Download files using JSON Formatting

SharePoint Online: Download files using JSON Formatting

You can use JSON formatting in SharePoint to customize the display of columns and views in SharePoint online lists or Microsoft Lists. In this blog post, I will explain how to add a button in document library view using JSON formatting which will allow you to download the file.

When you open the SharePoint online document library, by default the Download option for documents is not visible. So, if you want to download the file from SharePoint online modern document libraries, you have to select the file and use Download option from command bar at the top of document library OR from the context menu under ellipses ().

SharePoint Online modern experience - download file from document library
SharePoint Online – download file from document library

Downloading files using above SharePoint out of the box method requires multiple clicks and sometimes new SharePoint users might not be able to find the download option quickly. So, we will be using SharePoint JSON column formatting to add a button in document library view to download the file and avoid multiple clicks. Also, this button will be visible directly in document library column against each document so all users can quickly find the option to download the document.

You can use below JSON to add a button in SharePoint online document library column to download the file. This JSON formatting can be applied to any existing column in your document library OR if you want to create a new column and then apply JSON formatting, follow the steps given in this blog: Working with SharePoint Online/Microsoft List Comments using JSON Formatting 

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
    "elmType": "button",
    "style": {
        "border-radius": "5px",
        "margin": "5px 0px",
        "padding": "0px",
        "visibility": "=if([$ContentType] == 'Folder', 'hidden', 'visible')"
    },
    "attributes": {
        "class": "ms-bgColor-themePrimary"
    },
    "children": [
        {
            "elmType": "a",
            "txtContent": "Download",
            "style": {
                "text-decoration": "none",
                "padding": "10px 0px",
                "width": "100%"
            },
            "attributes": {
                "href": "= @currentWeb + '/_layouts/15/download.aspx?UniqueId=' + [$UniqueId]",
                "target": "_blank",
                "class": "ms-fontColor-white"
            }
        }
    ]
}

Where [$ContentType] and [$UniqueId] are internal names of Content type and Unique ID columns in SharePoint document library.

This SharePoint JSON formatting code adds a button within a document library view which downloads the file:

SharePoint Online - Download files from document library using SharePoint JSON Formatting modern experience
SharePoint Online – Download files using JSON Formatting

Note: The download functionality does not work for folder content type. So, in order to hide the download button for folders, you may have to show the Content Type column in your library view.

Above JSON is also available on GitHub in PnP List Formatting Repository at: Download File Button.

Learn More

13 thoughts on “SharePoint Online: Download files using JSON Formatting

Leave a comment