0

I am using JSON column formatting to generate a dynamic URL based on the value in another column.

The dynamic URL is a link to a document library which is filtered by the value in the other column.

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "style": {
        "margin-left": "0px"
      },
      "elmType": "a",
      "txtContent": "Click To View",
      "attributes": {
        "target": "_blank",
        "href": "='https://tenant-name.sharepoint.com/sites/site-name/cd/Forms/My%20View%20Name.aspx?env=WebViewList&FilterField1=My_x0020_Reference&FilterValue1=' + [$Title]"
      }
    }
  ]
}

The links are displaying correctly when I hover over them with my mouse, eg:

https://tenant-name.sharepoint.com/sites/site-name/cd/Forms/My%20View%20Name.aspx?env=WebViewList&FilterField1=My_x0020_Reference&FilterValue1=A & B Services - A & B Family Trust

However when I click on them, they seem to break when there are ampersands & in the column value.

So clicking on the above link loads this URL (note the URL is cut short after %20):

https://tenant-name.sharepoint.com/sites/site-name/cd/Forms/My%20View%20Name.aspx?env=WebViewList&FilterField1=My_x0020_Reference&FilterValue1=A%20

I have previously used the site below to better understand encoding and decoding HTML entities:

https://mothereff.in/html-entities

Should I be encoding the column value when passing it into the dynamic URL, and if so how can I do that in JSON column formatting?

Edit 1:

Based on this answer, I tried replacing:

"href": "='https://tenant-name.sharepoint.com/sites/site-name/cd/Forms/My%20View%20Name.aspx?env=WebViewList&FilterField1=My_x0020_Reference&FilterValue1=' + [$Title]"

with:

"href": {
"operator": "+",
"operands": [
"https://tenant-name.sharepoint.com/sites/site-name/cd/Forms/My%20View%20Name.aspx?env=WebViewList&FilterField1=My_x0020_Reference&FilterValue1=",
"[$Title]"
]
}

But it produced the same result.

Edit 2:

When I use the filters panel in the target document library to filter for the same value, I can see that SharePoint has converted & to %26 in the resulting URL.

So perhaps I need to somehow URL encode the column value before adding it to the dynamic URL?

Are there any operators that can do this in JSON column formatting?

Perhaps toString() would encode the value?

Is there anyway to use encodeURIComponent() in JSON column formatting?

Edit 3:

I tried using the replaceAll() operator to replace & with %26 and it seems to work.

Is there somehow I could use this approach to replace ALL the characters that SharePoint requires to be encoded?

"href": "='https://tenant-name.sharepoint.com/sites/site-name/cd/Forms/My%20View%20Name.aspx?env=WebViewList&FilterField1=My_x0020_Reference&FilterValue1=' + replaceAll([$Title], '&', '%26')"

1 Answer 1

1

Unfortunately, there is no equivalent operator in JSON formatting to encodeURIComponent() function in JavaScript.

So, you need to replace all special characters that SharePoint requires to be encoded using the replaceAll operator.

Reference: SharePoint: Replace All Occurrences of Substring in a String using JSON Formatting

4
  • Do you think it would be possible to 1) Define an exhaustive list of all the characters SharePoint doesn’t like 2) Use replaceAlll() to replace all those characters, not just one. Sep 3, 2022 at 12:23
  • 1
    @pointerless, thanks for sharing it which will help others in future. user1063287, I know it is difficult to use replaceAll for all characters but not impossible AND we have to use this workaround for now because SharePoint JSON formatting does not support any equaivalent operator for encodeURIComponent() in JS. Dec 13, 2023 at 12:11
  • This presently doesn't function for me, will debug then repost Dec 13, 2023 at 12:12
  • 1
    Needed to escape a ' replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(@currentField, '%' , '%25'), ':', '%3A'), '/', '%2F'), '?', '%3F'), '#', '%23'), '[', '%5B'), ']', '%5D'), '@', '%40'), '!', '%21'), '$', '%24'), '&', '%26'), '\\'', '%27'), '(', '%28'), ')', '%29'), '*', '%2A'), '+', '%2B'), ',', '%2C'), ';', '%3B'), '=', '%3D'), ' ', '%20') Dec 13, 2023 at 12:24

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.