1

The code below looks at my Client Number Field and if its greater then 5000, it concatenates and creates a hyperlink to a Jira item. This is working correctly. I need to add a "Then" statement, so if Client Number is less then 5000 then ignore and leave blank. They should be blank because the user will manually enter the the Jira URL and Jira number. When manually entered the data is not being displayed.

The statement I think should be:

If Client Number is Less then 5000 ignore, if Client Number is greater then 5000, then create url.

Any help would be appreciated?

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "children": [
      {
      "elmType": "a",
      "txtContent": "='XYZ-' + [$Client_Number]",
      "attributes": {
        "target": "_blank",
        "href": "='https://jira.com/browse/' + 'XYZ-' + [$Client_Number]"
      },
      "style": {
        "display": "=if([$Client_Number] <= 5000,  'none', '')"
      }
    }
  ]
}

3 Answers 3

0

Try using this JSON:

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
    "elmType": "div",
    "style": {
        "display": "=if([$Client_Number] <= 5000, 'none', 'block')"
    },
    "children": [
        {
            "elmType": "a",
            "txtContent": "='XYZ-' + [$Client_Number]",
            "attributes": {
                "target": "_blank",
                "href": "='https://jira.com/browse/' + 'XYZ-' + [$Client_Number]"
            }
        }
    ]
}

OR

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
    "elmType": "div",
    "children": [
        {
            "elmType": "a",
            "txtContent": "='XYZ-' + [$Client_Number]",
            "attributes": {
                "target": "_blank",
                "href": "='https://jira.com/browse/' + 'XYZ-' + [$Client_Number]"
            },
            "style": {
                "display": "=if([$Client_Number] <= 5000, 'none', 'block')"
            }
        },
        {
            "elmType": "span",
            "txtContent": "@currentField",
            "style": {
                "display": "=if([$Client_Number] <= 5000, 'block', 'none')"
            }
        }
    ]
}

Update:

If you want to convert @currentField into clickable hyperlink, use below JSON:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "elmType": "a",
      "txtContent": "='XYZ-' + [$Client_Number]",
      "attributes": {
        "target": "_blank",
        "href": "='https://jira.com/browse/' + 'XYZ-' + [$Client_Number]"
      },
      "style": {
        "display": "=if([$Client_Number] <= 5000, 'none', 'block')"
      }
    },
    {
      "elmType": "a",
      "txtContent": "@currentField",
      "attributes": {
        "target": "_blank",
        "href": "@currentField"
      },
      "style": {
        "display": "=if([$Client_Number] <= 5000, 'block', 'none')"
      }
    }
  ]
}

Here, Client_Number is the internal name of your SharePoint list column.

2
  • Thank you ! This is great! The second set of code worked - But what was display an url (no hyperlink). Example "jira.com/browse/XYZ-3985" instead of a hyperlinked "XYZ-3985" - This makes sense because when the user manually enters values for the field (HyperLink Field in SharePoint) they enter a Web Address and a Description -for this example it would be Web Address - jira.com/browse/XYZ-3985 Description XYZ-3985 it looks like its showing Web Address Below is my failed attempt "attributes": { "target": "_blank", "href": "='' + @currentField"
    – Melissa C
    May 24, 2023 at 20:30
  • Can you show us the screenshot of list view? What are the different columns you are trying to use in JSON, in which columns the hyperlink and description is entered and on which column are you applying this JSON. Screenshot will really help us to understand it better. You can blur out/cover any sensitive data if needed while posting the screenshot. May 25, 2023 at 6:01
0

Update: This code works for me and shows the hyperlink as expected.

Answer:

To add the Then statement in your column formatting code, you can use the if function to conditionally render the hyperlink only if the Client Number is greater than 5000. Here's the modified code:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "elmType": "a",
      "txtContent": "'XYZ-' + [$Client_Number]",
      "attributes": {
        "target": "_blank",
        "href": "='https://jira.com/browse/' + 'XYZ-' + [$Client_Number]"
      },
      "style": {
        "display": {
          "operator": "?",
          "operands": [
            {
              "operator": "<=",
              "operands": [
                "[$Client_Number]",
                5000
              ]
            },
            "none",
            "block"
          ]
        }
      }
    }
  ]
}

If the Client_Number is less than or equal to 5000, the display style is set to 'none', hiding the hyperlink. Otherwise, it is set to 'block', showing the hyperlink.

3
  • This hide all of the links
    – Melissa C
    May 24, 2023 at 20:35
  • This is the same JSON provided by Melissa. Only you have written it in AST format and added block value for CSS property. May 25, 2023 at 7:19
  • MelissaC, @Ganesh Answer updated with another version of the code.
    – Pradip R.
    May 25, 2023 at 7:52
0

Thank you everyone for your help!!! @Genesh Thanks for pointing me in the right direction! I edit your last code update and added .desc to @currentField.desc in txtContent

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "elmType": "a",
      "txtContent": "='XYZ-' + [$Client_Number]",
      "attributes": {
        "target": "_blank",
        "href": "='https://jira.net/browse/' + 'XYZ-' + [$Client_Number]"
      },
      "style": {
        "display": "=if([$Client_Number] <= 5000, 'none', 'block')"
      }
    },
    {
      "elmType": "a",
      "txtContent": "@currentField.desc",
      "style": {
        "display": "@currentField.desc"
      },
      "attributes": {
        "target": "_blank",
        "href": "='' + @currentField"
      }
    }
  ]
}

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.