0

Environment: SharePoint 2019 On Premise

Requirement: If a Number column value is equal to 0 then set sp-field-severity--good else set sp-field-severity--warning

Trial: Based on the article at Use column formatting to customize SharePoint have prepared and set the below JSON

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v1/column-formatting.schema.json",             
  "elmType": "div",
  "txtContent": "@currentField",
  "attributes": {
    "class": "=if(@currentField <= 0,'sp-field-severity--good', 'sp-field-severity--warning')"
  }
}

However it doesn't work.

I tried to check the DOM it shows as bellow

enter image description here

Note: As per the instructions in MS article I am using https://developer.microsoft.com/json-schemas/sp/v1/column-formatting.schema.json schema as it is SP 2019 On Prem.

What am I missing?

0
1

All Excel-style expressions begin with an equal (=) are only available in SharePoint Online (not in SharePoint 2019).

In SharePoint 2019, you have to use the Abstract Syntax Tree (AST) expressions.

So try using below JSON:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v1/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "@currentField",
  "attributes": {
    "class": {
      "operator": "?",
      "operands": [
        {
          "operator": "<=",
          "operands": [
            "@currentField",
            0
          ]
        },
        "sp-field-severity--good",
        "sp-field-severity--warning"
      ]
    }
  }
}

You can change the inner operator (<=) as per your requirements/conditions. For Example: To set the class attribute if number column value is equal to 0, use:

"class": {
  "operator": "?",
  "operands": [
    {
      "operator": "==",
      "operands": [
        "@currentField",
        0
      ]
    },
    "sp-field-severity--good",
    "sp-field-severity--warning"
  ]
}

Output:

enter image description here

3
  • 1
    Thankx a ton. It worked like a charm :) – shoab May 11 at 10:12
  • Hi Ganesh, how can i generate AST for a condition which has 3 options. That is If a=='1' then abc else if a=='2' the def and else if a=='3' then xyz – shoab May 13 at 7:10
  • You need to add multiple blocks of operator & operands. Check official documentation & List formatting samples for more information. – Ganesh Sanap May 13 at 7:36

Your Answer

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.