2

I'm working in a SharePoint library that contains both file and folder-type items.

I'm trying to replace the standard formatting for folders in a Name column with a clickable standard hyperlink. (The intention is to allow users can middle-click to open folders in a new window.)

I can cause the whole link to disappear if the content type matches my custom folder content type.

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "a",
  "txtContent": "@currentField",
  "attributes": {
    "href": "='AllItems.aspx?id=' + @currentField"
  },
  "style": {
    "visibility": "=if([$ContentTypeId]=='0x***mycontenttypeid***','visible', 'hidden')"
  }
}

However, this breaks navigation for file items on two counts:

  • file links simply don't appear
  • even if they did, the link in the href attribute would not work for a file item

What I'd like is to fallback to the default formatting for non-folder types. Is this possible? If not, will I have to construct a URL to display files, and how? (The defaultClick action only seems to work on button, not a elements.)

1 Answer 1

3

Use JSON code similar to this:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "elmType": "a",
      "txtContent": "@currentField",
      "attributes": {
        "href": "='AllItems.aspx?id=' + @currentField"
      },
      "style": {
        "display": "=if([$ContentType]=='Folder','block', 'none')"
      }
    },
    {
      "elmType": "div",
      "txtContent": "@currentField",
      "customRowAction": {
        "action": "defaultClick"
      },
      "style": {
        "display": "=if([$ContentType]=='Folder','none', 'block')"
      }
    }
  ]
}

You can apply conditions based on either [$ContentType] or [$ContentTypeId] as per your convenience.

Output:

enter image description here

Documentation: Use column formatting to customize SharePoint

3
  • 1
    Perfect, thanks - I hadn't realised it was possible to apply the defaultClick action to a div, not just a button.
    – Spinner
    Nov 15, 2021 at 16:58
  • For me [$ContentType] is empty, [$ContentTypeId] is not, why? I had to do "display": "=if(substring([$ContentTypeId], 0,5)=='0x012','none', 'block')" Sep 19, 2023 at 9:58
  • 1
    You have to show/include content type in documet library view in order to refer it's value in JSON formatting. Sep 19, 2023 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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