2

I am trying to use JSON to format a "Person" column in a Sharepoint list with an email action button.

I would like the email to contain a link to the Sharepoint list item in question, but the link contains ampersands (&).

Unfortunately the ampersand is a special character and chops the string after the 1st ampersand (as described here). Is there a way to stop this by getting the & to be treated as a symbol rather than a special character?

2
  • email action button - do you mean <a> with mailto:? Jan 17, 2019 at 9:26
  • Perhaps try the replaceAll() operator in JSON column formatting, eg replaceAll([$myColumnName], '&', '%26'). This works well when you know specifically what you are trying to find and replace, personally I am still looking for a solution that replaces ALL the characters that SharePoint needs to be encoded, there must be a smart regex-minded person out there who could figure that out :) Sep 3 at 7:45

5 Answers 5

2

You could try using URL encoding (percent encoding), replacing the & with %26. You can find all encoded characters on W3C.

1
  • Are there any operators available in JSON column formatting that can URL encode a string to make it an acceptable value in a SharePoint list link? Sep 3 at 7:21
1

You should encode only the body of your message. Use this service to build your url. Fill in required fields and then copy the value of href attribute from standard link from Output section (NOT encoded one).

For example below formatting works well for me (I use current item ID to build the url):

{
  "schema": "https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json",
    "elmType": "a",
    "attributes": {
      "target": "_blank",
      "href": "='mailto:some@user.com?subject=Hello&body=https%3A//myurl.com%3Fparam1%3D1%26param2%3D2%26ID%3D' + [$ID] + '\n another text'"
    },
    "txtContent": "[$Title]"
} 
1

You can replace & with %26 code in mailto:

You can refer below code:

<p>
    This is an email link:
    <a href="mailto:someone@example.com?Subject=Hello%20again&body=Test%26Test" target="_top">Send Mail</a>
</p>

More details about URL Encoding you can refer This

0

The answer here is to use operators and operands instead of a + on a single line

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
    "elmType": "div",
    "children": [
        {
            "elmType": "span",
            "style": {
                "padding-right": "8px"
            },
            "txtContent": "@currentField.title"
        },
        {
            "elmType": "a",
            "attributes": {
                "iconName": "Mail",
                "class": "sp-field-quickActions",
                "href": {
                    "operator": "+",
                    "operands": [
                        "mailto:",
                        "@currentField.email",
                        "?subject=Task status&body=Hey, how is your task coming along?.\r\n---\r\n",
                        "@currentField.title",
                        "\r\nClick this link for more info. http://contoso.sharepoint.com/sites/ConferencePrep/Tasks/Prep/DispForm.aspx?ID=",
                        "[$ID]"
                    ]
                }
            }
        }
    ]
}
0

SharePoint JSON formatting now supports replace and replaceAll operators which will help you to replace & with %26.

For example (using replaceAll operator):

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
    "elmType": "div",
    "txtContent": "=replaceAll([$Title], '&', '%26')",
    "style": {
        "font-weight": "bold"
    }
}

References:

  1. SharePoint: Replace All Occurrences of Substring in a String using JSON Formatting
  2. JSON formatting syntax reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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