Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
6.1k views
in SharePoint Online by 13 13 19

How to apply if else JSON SharePoint Column Formatting to format background color for a SharePoint column with the below nested if conditions

  • If current field is "Completed" set the background to blue.
  • Else IF current field is "Late" set the background to red.
  • Else IF current field is "Hold" set the background to Orange.
  • Else set the background to white.
{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "@currentField",
  "style": {
    "background-color": "=if(@currentField == 'Completed', 'Blue', '')"
  }
}

So how to perfrom else part for multiple consditions in JSON SharePoint Column Formatting?


1 Answer

1 like 0 dislike
by 163 204 403
selected by
 
Best answer

How to apply Nested IF in JSON for SharePoint SharePoint Column Formatting?

To perform IF-Else in JSON SharePoint SharePoint Column Formatting, you have to add at the second If condition at the else result of the first condition as following:

Consider this the syntax of the JSON If-Else condition as below:

if(your condition, 'if Yes', 'If No')

So to add an additional If-Else condition, you have to remove the 'If No' section, then replace it with the second if condition if(your condition, 'if Yes', 'If No') to be like this:

if(your condition, 'if Yes', if(your condition, 'if Yes', 'If No'))

Now, Let's apply multiple If-Else conditions in JSON, so we will remove 'If No' and add our new condition if(your condition, 'if Yes', 'If No') and repeate the same step for each If-Else like the following:

if(your condition, 'if Yes',
 if(your condition, 'if Yes', 
 if(your condition, 'if Yes', 
 if(your condition, 'if Yes',
 if(your condition, 'if Yes', 
 if(your condition, 'if Yes', 'If No'))))))

The above example for adding 6 IF-ELSE conditions in JSON SharePoint SharePoint Column Formatting


Final IF-ElSE JSON for SharePoint SharePoint Column Formatting

Let's apply Multiple IF-Else conditions based on your question

 =if(@currentField == 'Completed', 'Blue', if(@currentField == 'Late', 'Red', if(@currentField == 'On Hold', 'Orange', 'White')))

Final Result

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "@currentField",
  "style": {
    "background-color": "=if(@currentField == 'Completed', 'Blue', if(@currentField == 'Late', 'Red', if(@currentField == 'On Hold', 'Orange', 'White')))",
    "color": "White"
  }
}

Add the above code in your SharePoint column format, click Preview, and save if it matches your needs.

if else json sharePoint column format

Output: IF-ElSE JSON for SharePoint SharePoint Column Formatting

Great, the IF-ELSE JSON code for SharePoint column format has been applied as shown below:

background JSON sharepoint format based on multiple conditions


See Also

If you don’t ask, the answer is always NO!
...