How to write an if OR statement using abstract syntax tree for column formatting in SP 2019

Chris Eccles 0 Reputation points
2023-03-29T13:25:25.2866667+00:00

So I’m at a bit of a loss… I don’t really fancy doing nested if statements to cater for dealing with multiple options and I’d like to group them into an IF/OR statement but I can’t seem to get my head around how to use the “||” operator.

When I use this JSON, I get the debug error of “Failure: Must specify elmType.”

It seems that where I believe the true/false part goes, it doesn’t seem to be picking up those objects?

Im pretty much a novice, but I’ve been able to get so far up until now. Any help would be greatly appreciated. 😊

The operators at the top part work fine (selecting the class to apply) it’s the part under the “children” object that I’m struggling with.

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v1/column-formatting.schema.json",
  "debugMode": "true",
  "elmType": "div",
  "attributes":
  {
    "class":
    {
      "operator": "?",
      "operands": [
        {
          "operator": "==",
          "operands": [
            "@currentField", "Registered"
          ]
        },
        "sp-field-severity--warning",
        {
          "operator": "?",
          "operands": [
            {
              "operator": "==",
              "operands": [
                "@currentField", "Confirmed"
              ]
            },
            "sp-field-severity--low",
            {
              "operator": "?",
              "operands": [
                {
                  "operator": "==",
                  "operands": [
                    "@currentField", "Attended"
                  ]
                },
                "sp-field-severity--good",
                "sp-field-severity--blocked"
              ]
            }
          ]
        }
      ]
    }
  },
  "children": [
    {
      "operator" : "?",
      "operands" : [
        {
          "operator": "||",
          "operands": [
                        {
                                      "operator": "==",
                                      "operands": ["@currentField", "Attended"]
                        },
            {
              "operator": "==",
                                      "operands": ["@currentField", "Not Attended"]
                        }
                      ]
                    },
                    {
                      "elmType": "span",
          "txtContent": "@currentField",
          "style": {
            "width": "100%",
            "text-align": "center"
          }
                    },
                    {
          "elmType": "a",
          "txtContent": "@currentField",
          "style": {
            "width": "100%",
            "text-align": "center"
          },
          "attributes": {
            "target": "_blank",
            "href": {
              "operator": "?",
              "operands": [
                {
                  "operator": "==",
                  "operands": ["@currentField", "Registered"]
                },
                "http://xxxx.xxxx.xxxx.xxxx/sites/SPxxxx/Lists/Tasks/Registrations.aspx",
                {
                  "operator": "?",
                  "operands": [
                    {
                      "operator": "==",
                      "operands": ["@currentField", "Confirmed"]
                    },
                    "http://xxxx.xxxx.xxxx.xxxx/sites/SPxxxx/Lists/Tasks/Attendance%20Confirmation.aspx",
                    ""
                  ]
                }
              ]
            }
          }
               }
      ]
    }
  ]
}

Microsoft 365 and Office SharePoint For business Windows
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-03-30T07:04:04.6233333+00:00

    Hi @Chris Eccles

    You can refer to following json to write if or statement

    {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
      "elmType": "div",
      "attributes": {
        "class": "=if(@currentField == 'Done', 'sp-field-severity--good', if(@currentField == 'In progress', 'sp-field-severity--low', if(@currentField == 'In review', 'sp-field-severity--warning', if(@currentField == 'Has issues', 'sp-field-severity--severeWarning', 'sp-field-severity--blocked')))) + ' ms-fontColor-neutralSecondary'"
      },
      "children": [
        {
          "elmType": "span",
          "style": {
            "display": "inline-block",
            "padding": "0 4px"
          },
          "attributes": {
            "iconName": "=if(@currentField == 'Done', 'CheckMark', if(@currentField == 'In progress', 'Forward', if(@currentField == 'In review', 'Error', if(@currentField == 'Has issues', 'Warning', 'ErrorBadge'))))"
          }
        },
        {
          "elmType": "span",
          "txtContent": "@currentField"
        }
      ]
    }
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.