Search code examples
jsonsharepointsharepoint-onlinesharepoint-list

Conditional formatting on SharePoint list column with Flow buttons


I'm trying to include a button to conditionally launch a flow on an item of a SharePoint List.

I have a list with multiple columns, such as Status, Approval and Created_By. I want the Approval column to show buttons that can launch a Power Automate Flow on the list item. And these buttons should be different in the following situations:

  1. When the field in the Status column equals 'Draft' -> Show button to start Flow 1
  2. When the field in the Status column equals 'Approved' -> Show button to start Flow 2
  3. When the person who views the list is not the creator of the item -> Show no button

The method to include a button to launch a flow, using a formatted column, can be found here. However the example does not include conditional statements, see code below:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "button",
  "customRowAction": {
    "action": "executeFlow",
    "actionParams": "{\"id\": \"edf627d9-20f4-45ba-8bc9-4494bf2ff1be\"}"
  },
  "attributes": {
    "class": "ms-fontColor-themePrimary ms-fontColor-themeDarker--hover"
  },
  "style": {
    "border": "none",
    "background-color": "transparent",
    "cursor": "pointer"
  },
  "children": [
    {
      "elmType": "span",
      "attributes": {
        "iconName": "Flow"
      },
      "style": {
        "padding-right": "6px"
      }
    },
    {
      "elmType": "span",
      "txtContent": "Send to Manager"
    }
  ]
}

I found that it is possible to include if statements in the JSON code. The Status can be checked with the following code: "=if([$Status] == 'Approved','','')".

Whether the person who is viewing the list is the same as the person who created the item should be possible to check with something like this (although this specific piece of code does not work..): "=if(@me == [$Created_x0020_By.email],'','')"

I'm not really known with JSON so I don't know how to insert these conditions into the code above. So I'd like to ask your help!


Solved thanks to Michael Han_MSFT! My resulting code is:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "button",
  "customRowAction": {
    "action": "executeFlow",
    "actionParams": "=if(@me == [$Author.email], if([$Status]=='Draft', '{\"id\": \"d4ebc660-16fd-4320-a69c-d91d51277666\"}', if([$Status]=='Approved', '{\"id\": \"94233841-9c32-4e90-9018-479e72c33385\"}', 'null')), 'null')"
  },
  "attributes": {
    "class": "ms-fontColor-themePrimary ms-fontColor-themeDarker--hover"
  },
  "style": {
    "border": "none",
    "background-color": "transparent",
    "cursor": "pointer"
  },
  "children": [
    {
      "elmType": "span",
      "attributes": {
        "iconName": "=if(@me == [$Author.email],'Flow','')"
      },
      "style": {
        "padding-right": "6px"
      }
    },
    {
      "elmType": "span",
      "txtContent": "=if(@me == [$Author.email], if([$Status]=='Draft','Start approval', if([$Status]=='Approved','Cancel', '')), '')"
    }
  ]
}

Solution

  • 1.The Statu can be checked in the "actionParams" with the following code, different id for different flows:

    "actionParams": "=if([$Status]=='Approved','{\"id\": \"a04b0180-7d32-471c-8ff6-49497d229b23\"}','{\"id\": \"1f922b63-a66b-4177-9aa6-f4c37c4d6f27\"}')"
    

    2.You need to use [$Author.email] instead of [$Created_x0020_By.email].

    Below example code for your reference:

        {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
      "elmType": "button",
      "customRowAction": {
        "action": "executeFlow",
        "actionParams": "=if([$Status]=='Approved','{\"id\": \"a04b0180-7d32-471c-8ff6-49497d229b23\"}','{\"id\": \"1f922b63-a66b-4177-9aa6-f4c37c4d6f27\"}')"
      },
      "attributes": {
        "class": "ms-fontColor-themePrimary ms-fontColor-themeDarker--hover"
      },
      "style": {
        "border": "none",
        "background-color": "transparent",
        "cursor": "pointer"
      },
      "children": [
        {
          "elmType": "span",
          "attributes": {
            "iconName": "=if(@me == [$Author.email],'Flow','')"
          },
          "style": {
            "padding-right": "6px"
          }
        },
        {
          "elmType": "span",
          "txtContent": "=if(@me == [$Author.email], if([$Status]=='Approved','Flow1','Flow2'), '')"
        }
      ]
    }
    

    Test result:

    enter image description here