2

PrimaryLink is to display a hyperlink based on the other 2 fields

PrimaryName
PrimaryURL
PrimaryLink

JSON for Column Formatting

{
   "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
   "elmType": "a",
   "txtContent": "[$PrimaryName]",
   "attributes": {
   "target": "_blank",
   "href": "[$PrimaryURL]"
    }
}

So if I enter a PrimaryName and a PrimaryURL, it would display the PrimaryName as a hyperlink in PrimaryLink to what was set in PrimaryURL. If I only put a PrimaryName in, it still displays a hyperlink that goes to the /list/AllItems.aspx

Is there a way where if I don't populate PrimaryURL, that it can only display PrimaryName text and no hyperlink?

Tried this from a post recommended in comments, but it leaves the whole column empty.

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "a",
  "txtContent": "[$PrimaryName]",
  "attributes": {
    "target": "_blank",
    "href": "[$PrimaryURL]"
  }
    "style":{
        "display": "=if([$PrimaryURL]!='', 'block', 'none !important')"
    }
}
7
  • 2
    Does this answer your question? JSON control if @currentfield is empty
    – willman
    Mar 10 '20 at 14:42
  • html is no longer permissable in calculated columns - I notice you have used a <a href>
    – Tally
    Mar 10 '20 at 14:43
  • You don't need a calculated column here. You cannot create an anchor tag through calculated formulas any way. Use the answer to this question in your column formatting for your PrimaryName field to render conditionally based on PrimaryURL.
    – willman
    Mar 10 '20 at 14:44
  • Yes, the calculated column was leftover from the SP2013 on-prem solution. I also tried adding a text column and used my same JSON and it displays all hyperlinks, but wouldn't contain the proper link if I put in a PrimaryURL. So given that, I just left it as is. Mar 10 '20 at 15:31
  • I had tried the link that yalls have suggested as well. If I leave my href section and then add this, PrimaryLink is just empty. "style": { "display": "=if([$PrimaryURL]!='', 'block','none !important')", } Mar 10 '20 at 15:36
2

Use below JSON Code:

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
    "elmType": "div",
    "children": [
        {
            "elmType": "span",
            "txtContent": "[$PrimaryName]",
            "style": {
                "display": "=if([$PrimaryURL] != '', 'none', 'block')"
            }
        },
        {
            "elmType": "a",
            "txtContent": "[$PrimaryName]",
            "attributes": {
                "target": "_blank",
                "href": "[$PrimaryURL]"
            },
            "style": {
                "display": "=if([$PrimaryURL] != '', 'block', 'none')"
            }
        }
    ]
}

Output:

enter image description here

official documentation:

  1. Use column formatting to customize SharePoint
  2. Column formatting - Samples

Update from Comments:

  1. In order to reference a column in JSON formatting it should be included in list view.
  2. If you are adding the URL in PrimaryLink column and then adding the JSON in PrimaryName column then try below JSON:
{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
    "elmType": "div",
    "children": [
        {
            "elmType": "span",
            "txtContent": "@currentField",
            "style": {
                "display": "=if([$PrimaryLink] != '', 'none', 'block')"
            }
        },
        {
            "elmType": "a",
            "txtContent": "@currentField",
            "attributes": {
                "target": "_blank",
                "href": "[$PrimaryLink]"
            },
            "style": {
                "display": "=if([$PrimaryLink] != '', 'block', 'none')"
            }
        }
    ]
}
6
  • You are AWESOME! That worked a treat. So as I was doing a lot of testing, I was finding good resources scarce. Do you have some good places/sites/?? where I can improve my JSON? Mar 11 '20 at 18:21
  • 1
    Check the documentation link in updated answer. Mar 12 '20 at 0:26
  • Hmm, if I hide the PrimaryURL from view, the PrimaryName and PrimaryLink goes empty. Does that happen to you? Surely that can't be by design because that would defeat the purpose of views. Mar 31 '20 at 16:05
  • I think in order the JSON formatting to work, your column should be in the list view. I will check it tomorrow on my machine and let you know. Mar 31 '20 at 16:22
  • I tried to make the solution have less variables/columns, so tried to do PrimaryName and PrimaryLink. If I put in the URL in PrmaryLink and then put the JSON in PrimaryName, it's curious that when I refresh the page, it doesn't even display the text without the hyperlink. Apr 2 '20 at 20:42

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