2

Is it possible to have a link to attachments using json formatting?

I want my users to be able to click a link to go the the attachment file from a view and not have to edit or click on the form to see the attachments.

1 Answer 1

4

Unfortunately, the attachments field is not directly accessible in JSON formatting.

You can only check if attachments are available or not for the list item using:

"txtContent": "=if([$Attachments] == '0', 'no attachment', 'item has attachment')"

Source: Conditional formatting if list item has attachment


However, if you have the filename stored in one of the columns in list, you can build a link to the attachment using JSON formatting like:

{
  "$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([$Attachments] == '0', 'hidden', 'visible')"
  },
  "attributes": {
    "class": "ms-bgColor-themePrimary"
  },
  "children": [
    {
      "elmType": "a",
      "txtContent": "Attachments",
      "style": {
        "text-decoration": "none",
        "padding": "10px 0px",
        "width": "100%"
      },
      "attributes": {
        "href": "= @currentWeb + '/Lists/ListName/Attachments/' + [$ID] + '/' + [$FileName] + '?web=1'",
        "target": "_blank",
        "class": "ms-fontColor-white"
      }
    }
  ]
}

Output:

enter image description here

Similar thread: Clickable href with the attachments

4
  • Thanks i tried it but it not working. I can see you have the File Name field. What is that and how did you get that
    – naijacoder
    Commented Nov 24, 2022 at 23:11
  • That is the workaround because you cannot read attachments and file names using JSON. If you have file name in some other column (similar to mentioned in 2nd link in my answer), then you can use the above JSON. Commented Nov 25, 2022 at 3:30
  • File Name is the name of attached file with extension. I have added it manually just to showcase the workaround to you. You can either add it manually or use any automated/scheduled power Automate flow to get the item attachments and update the column with file names. Commented Nov 25, 2022 at 3:32
  • 1
    Thanks Ganesh, That was what i was thinking too
    – naijacoder
    Commented Nov 25, 2022 at 6:10

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.