Warn the user if a file is broken (not unzipped)

Learn how to warn the user if a file is broken (usually not unzipped) in a SharePoint library


The challenge

Drag and dropping files is a huge benefit of SharePoint libraries, but if the file dragged from a zipped file, it will look OK to the user, but actually it is not

SharePoint document library showing a file dragged from a zip archive that appears normal but has a file size of zero and is actually broken

The solution

Let’s create a JSON view formatting that warns the user by changing the color of a broken file to red

SharePoint document library with the broken file row highlighted in red using view formatting to alert the user

In the file name we will show a warning icon, and if the user hover the mouse over the icon we will tell them what is wrong

SharePoint file name column showing a warning icon next to the broken file, with a hover tooltip explaining the file must be unzipped before uploading

The code

To change the color of the broken file to red go to format current view

SharePoint library view options menu with Format current view selected to open the view formatting panel

Insert following code

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/view-formatting.schema.json",
  "additionalRowClass": "=if([$File_x0020_Size] == '0', 'sp-field-severity--blocked', '')"
}

To add the warning icon click on your column name -> column settings -> format this column

SharePoint column settings menu with Format this column selected to open the column formatting panel for the file name column

Insert following code

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "style": {
    "background-color": "transparent"
  },
  "attributes": {
    "class": "ms-fontColor-neutralSecondary ms-fontColor-neutralDark--hover ms-font-m"
  },
  "children": [
    {
      "elmType": "span",
      "attributes": {
        "iconName": "Warning",
        "class": "ms-font-l"
      },
      "style": {
        "padding-right": "6px",
        "display": "=if([$File_x0020_Size] == '0', 'inline', 'none')"
      },
      "customCardProps": {
        "openOnEvent": "hover",
        "formatter": {
          "elmType": "div",
          "children": [
            {
              "elmType": "div",
              "style": {
                "padding": "15px 45px 15px 15px"
              },
              "children": [
                {
                  "elmType": "div",
                  "style": {
                    "margin-bottom": "5px"
                  },
                  "children": [
                    {
                      "elmType": "span",
                      "style": {
                        "font-size": "16px",
                        "font-weight": "bold"
                      },
                      "txtContent": "Your file is broken"
                    },
                    {
                      "elmType": "div",
                      "style": {
                        "font-size": "14px",
                        "margin-top": "10px"
                      },
                      "txtContent": "='Your file ' + @currentField + ' must be unzipped before uploading'"
                    }
                  ]
                }
              ]
            }
          ]
        },
        "directionalHint": "rightCenter",
        "isBeakVisible": true
      }
    },
    {
      "elmType": "span",
      "txtContent": "@currentField"
    }
  ]
}

There you go - we can now help/warn the users if they forgot to unzip a file

SharePoint document library showing the final result with the broken file row in red and a warning icon with a hover tooltip on the file name


See also