Monday, March 19, 2018

SP json conditional formatting

Hi world!  Yes it has been a while indeed...but finally something to give back!

My customer asked to have SharePoint Online views formatted such that the font color for certain rows would be different based on the Status field value for the list item.  Since we're using SPO I did not want to go the custom column formatter route, and I found a reference for using JSON for column formatting - but no great examples.

The only wrinkle I found is this is done at the field level - so wherever this field is used in a list, it will include this formatting.  Fortunately not an issue in my case, but you might have a case where you want the field to retain its default formatting.

{
    "elmType": "div",
    "txtContent": "@currentField",
    "style": {
       "color": {
          "operator": "?",
          "operands": [
             {
                "operator": "==",
                "operands": [
                   "[$processStatus]",
                   "Inactive"
                ]
             },
             "#a9a9a9",
             ""
          ]
       }
    }
 }

My status field is processStatus (case matters).  And in my case I want to change the color to a dark gray, if the status is equal to 'Inactive'. 

The result:


Success!