Sharepoint JSON column formatting not working

Laurel Lagan 21 Reputation points
2021-06-04T19:45:59.653+00:00

I'm trying to use JSON to apply conditional formatting to a Choice column in SharePoint server 2019. My list contains a choice column titled "Cost Status" with the options: Red, Yellow, Green, N/A.

I want the column to be formatted the corresponding color and when N/A is selected have no special formatting. I have icons as part of the conditional formatting, but I'm not set on having those as the end solution. The most important part is the color formatting.

All that displays is the text, my list isn't showing any colors or icons in the column.

Any help is appreciated.

Here's my code:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v1/column-formatting.schema.json",
  "elmType": "div",
  "attributes": {
    "class": "=if(@currentField == 'Green', 'sp-field-severity--good', if(@currentField == 'N/A', 'sp-field-severity--low', if(@currentField == 'Yellow', 'sp-field-severity--warning', if(@currentField == 'Red', 'sp-field-severity--blocked')))) + ' ms-fontColor-neutralSecondary'"
  },
  "children": [
    {
      "elmType": "span",
      "style": {
        "display": "inline-block",
        "padding": "0 4px"
      },
      "attributes": {
        "iconName": "=if(@currentField == 'Green', 'CheckMark', if(@currentField == 'Yellow', 'Error', if(@currentField == 'Red', 'Warning', '')))"
      }
    },
    {
      "elmType": "span",
      "txtContent": "@currentField"
    }
  ]
}
SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,329 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,939 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,605 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jerry Xu-MSFT 7,956 Reputation points
    2021-06-07T01:53:08.693+00:00

    Hi, @Laurel Lagan

    Here is the difference on JSON formatting between SharePoint 2019 and SharePoint Online. Excel-style expressions are not supported on SP 2019. You need to use the AST(Abstract Syntax Tree) version of the JSON from the sample repository on SP 2019. In other words, the if combined conditions doesn't work as expected, we have to use the basic styled syntax.

    Here is the test code work in my end. You can have a try on it.

     {  
       "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",  
      
       "elmType": "div",  
       "attributes": {  
     "class": {  
       "operator": "+",  
       "operands": [  
     {  
       "operator": "?",  
       "operands": [  
     {  
       "operator": "==",  
       "operands": [  
     "@currentField",  
     "Green"  
       ]  
     },  
     "sp-field-severity--good",  
     {  
       "operator": "?",  
       "operands": [  
     {  
       "operator": "==",  
       "operands": [  
     "@currentField",  
     "N/A"  
       ]  
     },  
     "sp-field-severity--low",  
     {  
       "operator": "?",  
       "operands": [  
     {  
       "operator": "==",  
       "operands": [  
     "@currentField",  
     "Yellow"  
       ]  
     },  
     "sp-field-severity--warning",  
     {  
       "operator": "?",  
       "operands": [  
     {  
       "operator": "==",  
       "operands": [  
     "@currentField",  
     "Red"  
       ]  
     },  
     "sp-field-severity--severeWarning",  
     "sp-field-severity--blocked"  
       ]  
     }  
       ]  
     }  
       ]  
     }  
       ]  
     },  
     " ms-fontColor-neutralSecondary"  
       ]  
     }  
       },  
       "children": [  
     {  
       "elmType": "span",  
       "style": {  
     "display": "inline-block",  
     "padding": "0 4px"  
       },  
       "attributes": {  
     "iconName": {  
       "operator": "?",  
       "operands": [  
     {  
       "operator": "==",  
       "operands": [  
     "@currentField",  
     "Green"  
       ]  
     },  
     "CheckMark",  
     {  
       "operator": "?",  
       "operands": [  
     {  
       "operator": "==",  
       "operands": [  
     "@currentField",  
     "N/A"  
       ]  
     },  
     "Forward",  
     {  
       "operator": "?",  
       "operands": [  
     {  
       "operator": "==",  
       "operands": [  
     "@currentField",  
     "Yellow"  
       ]  
     },  
     "Error",  
     {  
       "operator": "?",  
       "operands": [  
     {  
       "operator": "==",  
       "operands": [  
     "@currentField",  
     "Red"  
       ]  
     },  
     "Warning",  
     "ErrorBadge"  
       ]  
     }  
       ]  
     }  
       ]  
     }  
       ]  
     }  
       }  
     },  
     {  
       "elmType": "span",  
       "txtContent": "@currentField"  
     }  
       ]  
     }  
    

    How it works like :
    102821-image.png

    You can find more examples of SharePoint 2019 in this link below.

    https://github.com/SharePoint/sp-dev-list-formatting/tree/master/column-samples

    Here is a thread talking about this difference.

    https://techcommunity.microsoft.com/t5/SharePoint-Developer/SharePoint-2019-column-formatting-not-working/m-p/253902

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    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.

    3 people found this answer helpful.

0 additional answers

Sort by: Most helpful

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.