0

I am new to JSON. I am trying to set up a "preferred contact info" column in a SharePoint list. I would like the field to accept a phone number, an e-mail address, or both. When an e-mail address is entered, I would like the column to display a clickable mailto: link. When a phone number is entered, I would like it to be formatted (000) 000-0000. So far I just have the code for e-mail addresses:

"$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json", 

"elmType": "div", 

"children": [ 

    { 

        "elmType": "a", 

        "style": { 

            "padding-right": "8px" 

        }, 

        "txtContent": "@currentField", 

    "attributes": { 

    "target": "_blank", 

    "href": { 

                "operator": "+", 

                "operands": [ 

                    "mailto:", 

                    "@currentField" 

                ] 

            } 

             

        } 

    }, 

    { 

        "elmType": "a", 

        "attributes": { 

    "target":"_blank", 



            "class": "sp-field-quickActions", 

    "href": { 

                "operator": "+", 

                "operands": [ 

                    "mailto:", 

                    "@currentField"


                ] 

            } 

             

        } 

    } 

] 
New contributor
NewJSON is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • How can JSON identify if it is email or phone number? Is there any specific format / domain being used in email which we can check in JSON? Aug 4 at 2:51

1 Answer 1

0

You can use following JSON

    {
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "elmType": "a",
      "style": {
        "padding-right": "8px",
        "display": "=if(indexOf(@currentField,'@') > -1, 'block','none')"
      },
      "txtContent": "@currentField",
      "attributes": {
        "target": "_blank",
        "href": {
          "operator": "+",
          "operands": [
            "mailto:",
            "@currentField"
          ]
        }
      }
    },
    {
      "elmType": "span",
      "style": {
        "overflow": "hidden",
        "text-overflow": "ellipsis",
        "padding": "0 3px",
        "display": "=if(indexOf(@currentField,'@') > -1, 'none','block')"
      },
      "txtContent": "=if(indexOf(@currentField + '^', '^') == 10,'('+substring(toString(@currentField),0,3)+') '+substring(toString(@currentField),3,6)+'-'+substring(toString(@currentField),6,10),if(indexOf(@currentField + '^', '^') == 7,substring(toString(@currentField),0,3)+'-'+substring(toString(@currentField),3,8),@currentField)",
      "attributes": {
        "class": ""
      }
    }
  ]
}

In case of Invalid mobile number, I have shown as it is.

Here I have used rules like if value contains "@" then I considered as email. As per Ganesh's comment, if you have fixed domain which you can use to identify whether value is email or not then you can adapt condition according.

Following is output of JSON formatting:

enter image description here

Reference Link for Mobile Number formatting: SharePoint Online JSON format phone column to display (555) 444-3333 or 444-3333

Please mark this as answer it it does help to you.

Your Answer

NewJSON is a new contributor. Be nice, and check out our Code of Conduct.

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.