pnp / List-Formatting

List Formatting Samples for use in SharePoint and Microsoft Lists
https://pnp.github.io/List-Formatting/
MIT License
1.74k stars 833 forks source link

I have a question about SAMPLE generic-hyperlink-values #584

Open oshihirii opened 2 years ago

oshihirii commented 2 years ago

Sample

https://github.com/pnp/List-Formatting/tree/master/column-samples/generic-hyperlink-values

Question

The code shown in the example above:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "a",
  "txtContent": "@currentField",
  "attributes": {
    "target": "_blank",
    "href": "='http://finance.yahoo.com/quote/' + @currentField"
  }
}

works well as long as the dynamic URL:

For example, I am trying to generate a dynamic URL based on the value in another column.

The dynamic URL is a link to a filtered view of a document library, eg:

"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]"
}

If the value in the other column includes an ampersand &, the URL breaks when clicking on the dynamic URL.

Is there anyway to URL encode the column value when adding it to the dynamic URL?

tecchan1107 commented 2 years ago

Hi @oshihirii ! How about replacing & with %26 using the replaceAll operator? I tried the following method of using the replaceAll operator and was able to filter successfully even if the value contained &.

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "elmType": "a",
      "txtContent": "@currentField",
      "attributes": {
        "href": "= 'https://<<tenantName>>.sharepoint.com/sites/<<siteName>>/<<documentLibraryName>>/Forms/<<View>>.aspx?FilterField1=Choice&FilterType1=Choice&FilterValue1=' + replaceAll(@currentField,'&','%26')",
        "target": "_blank"
      }
    }
  ]
}

image

tecchan1107 commented 2 years ago

The method without replaceAll did not filter properly for me as well.

oshihirii commented 2 years ago

Hi @tecchan1107 ,

Thankyou, it is a good idea and your approach is what I am implementing now.

For it to be a truly thorough solution, I am wondering if somebody smarter than me could figure out a replaceAll() that replaces all characters that SharePoint requires to be replaced in the URL.

That way we could be confident that any value we are grabbing from another column to create a filtered list URL (using JSON formatting) will be encoded as required.

I guess it would basically imitate an encodeURI() function.

The first step would be defining all the illegal characters in a SharePoint list URL.

I have searched and the only illegal characters I come across are those relating to file names, eg:

" * : < > ? / \ |

But I am not sure if they are the same characters that are not allowed in a SharePoint list URL.

Related Reading:

Restrictions and limitations in OneDrive and SharePoint > Invalid characters

Query String URL Tricks for SharePoint and Microsoft 365