0

I have a project list with the color status as Blue-PRJ1, Blue-PRJ2, Blue-PRJ3, Green-PRJ1, Green-PRJ2, Green-PRJ3, etc.

The color status column is the LookUp type column. When the user selects the Blue-PRJ1 or Blue-PRJ2 or Blue-PRJ3 from the lookup column, the line of the row will be changed to the respective color like Blue, Green, Red, etc.

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json",
    "style": {
    "font-style": "ms-fontColor-alert"
   },
  "additionalRowClass": "=if([$ProjectStatusLookupColumn.lookupValue] == 'Blue-PRJ1', 'ms-bgColor-blue',if([$ProjectStatusLookupColumn.lookupValue] == 'Blue-PRJ2', 'ms-bgColor-blue',if([$ProjectStatusLookupColumn.lookupValue] == 'Blue-PRJ3', 'ms-bgColor-blue','')))"
}

I am handling this using the multiple if condition which is working fine. Now, I am trying to reduce the multiple if condition and use something like start with text.....for example, if the lookup column value starts with the Blue, then that line be blue, if starts with Green then that line is Green, and so on.

Is anyone implemented similar requirements or how can we handle the condition like "start with" in the lookup column in JSON formatting - SharePoint Online list?

2 Answers 2

1

Use below JSON code, it should work for you:

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/view-formatting.schema.json",
    "style": {
        "font-style": "ms-fontColor-alert"
    },
    "additionalRowClass": "=if(startsWith([$ProjectStatusLookupColumn.lookupValue], 'Blue'), 'ms-bgColor-blue', if(startsWith([$ProjectStatusLookupColumn.lookupValue], 'Red'), 'ms-bgColor-red', if(startsWith([$ProjectStatusLookupColumn.lookupValue], 'Green'), 'ms-bgColor-green', '')))"
}

Documentation: JSON Formatting syntax reference

3
  • 1
    Thank you, Ganesh, it worked well.
    – SP 2022
    Jul 29 at 16:09
  • You're welcome. Please Upvote(^) as it helped you & it will help others with similar question in future to find the correct answer easily. Aug 1 at 5:58
  • 1
    Upvote Done :-)
    – SP 2022
    Aug 1 at 6:42
1

Column formatting does have start with method.

You can use following method in expression:

https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json

enter image description here

I have prepared JSON Formatting according to your requirement.

    {
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "style": {
    "box-sizing": "border-box",
    "padding": "0 2px",
    "overflow": "hidden",
    "text-overflow": "ellipsis"
  },
  "attributes": {
    "class": {
      "operator": ":",
      "operands": [
        {
          "operator": "startsWith",
          "operands": [
            "[$ProjectStatusLookupColumn.lookupValue]",
            "Blue"
          ]
        },
        "sp-css-backgroundColor-BgCornflowerBlue sp-field-fontSizeSmall sp-css-color-CornflowerBlueFont",
        {
          "operator": ":",
          "operands": [
            {
              "operator": "startsWith",
              "operands": [
                "[$ProjectStatusLookupColumn.lookupValue]",
                "Green"
              ]
            },
            "sp-css-backgroundColor-BgGreen sp-css-borderColor-CyanFont sp-field-fontSizeSmall sp-css-color-WhiteFont",
            {
              "operator": ":",
              "operands": [
                {
                  "operator": "startsWith",
                  "operands": [
                    "[$ProjectStatusLookupColumn.lookupValue]",
                    "Red"
                  ]
                },
                "sp-css-backgroundColor-BgRed sp-css-borderColor-CyanFont sp-field-fontSizeSmall sp-css-color-WhiteFont",
                ""
              ]
            }
          ]
        }
      ]
    }
  },
  "txtContent": "[$ProjectStatusLookupColumn.lookupValue]"
}

It will give output as below

enter image description here

Please mark this as answer if it works for you.

5
  • Thanks for your reply @Kalpesh - I have just used the second part of your answer (JSON code) it didn't work, that is "I have prepared JSON Formatting according to your requirement." Do I need to include the black color top portion code as well?
    – SP 2022
    Jul 29 at 10:33
  • Black color screen shot is just for the reference Jul 29 at 10:37
  • Not sure why - the JSON code is not working for me - just I am copy-pasting your JSON code but it is not working.
    – SP 2022
    Jul 29 at 12:41
  • Make sure that you Update. exact SharePoint's field's internal name in the JSON. Can you add screen shot from SharePoint for JSON Formatting screen shot as well Jul 29 at 12:45
  • Hi @Kalpesh - your answer is well descriptive and you deserve the upvote :-) not sure somehow I couldn't make this work on my site, but the other answer actually worked for me. Eager to accept both but the system will not allow it. Thank you. :-)
    – SP 2022
    Jul 29 at 16:06

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.