4

I'm using JSON to format a hyperlink column in SharePoint online. If the column @currentfield isn't filled in, there shouldn't be a link. Can anyone help me?

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "a",
  "txtContent": "@currentField",
  "attributes": {
    "target": "_blank",
    "href": "='https://contoso.sharepoint.com/w' + @currentField"

  }

2 Answers 2

4

You can achieve this using a style tag with a conditional, see below:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "a",
  "txtContent": "@currentField",
  "attributes": {
    "target": "_blank",
    "href": "='https://contoso.sharepoint.com/w' + @currentField"
  "style": {
    "display": "=if(@currentField!='', 'block','none !important')",
  }
}

Though personally, I like a good link icon, so I format mine like so:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "elmType": "a",
      "attributes": {
        "target": "_blank",
        "iconName": "Link",
        "class": "sp-field-quickActions",
        "href": "=if(@currentField!='',@currentField,''"
      },
      "style": {
        "display": "=if(@currentField!='', 'block','none !important')",
        "padding-left": "40px"
      }
    }
  ]
}

Hope that helps!

1
  • Thanks for your solution! Its working great for me :-) Commented May 16, 2019 at 10:11
1

I did not find the way to format the field as a hyperlink if it is not empty only.

The way I can think of implementing this requirement is to turn the empty field to a link which is used to refresh the page.

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "a",
  "txtContent": "@currentField",
  "attributes": {
    "target": "=if(@currentField!='','_blank', '_self')",
    "href": "=if(@currentField!='','https://testlz.sharepoint.com/sites/' + @currentField, '')"
  }
}

JSON formatting in SharePoint Online

1
  • any progress on this? I'm trying to accomplish the same thing with column formatting.
    – Haymak3r
    Commented Oct 29, 2018 at 20:11

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.