2

I want to show button A or button B in a column depending on value in another column.

one button should start a flow, another button should launch a hyperlink also I need to add other buttons.

I have spent the day googling and fudging and the current code shows nothing, if I remove the code for button B then button A works.

{
    "elmType": "div",
    "children": [
        {
            "elmType": "button",
            "txtContent": "New ",
            "customRowAction": {
                "action": "executeFlow",
                "actionParams": "{\"id\": \"be40d280-88df9a875fa\"}"
            },
            "style": {
                "display": "=if(  ([$Kent] == 'A' ), 'block', 'none')"
            }
        },
        {
            "elmType": "button",
            "txtContent": "New",
            "attributes": {
                "target": "_blank",
                "href": "='URL'"
            },
            "style": {
                "display": "=if(([$Kent] == 'B'), 'block', 'none')"
            },
        }
    ]
}

2 Answers 2

1

Try using below JSON code, it should work for you:

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
    "elmType": "div",
    "children": [
        {
            "elmType": "button",
            "txtContent": "New ",
            "customRowAction": {
                "action": "executeFlow",
                "actionParams": "{\"id\": \"be40d280-88df9a875fa\"}"
            },
            "style": {
                "display": "=if([$Kent] == 'A', 'block', 'none')"
            }
        },
        {
            "elmType": "a",
            "txtContent": "New",
            "attributes": {
                "target": "_blank",
                "href": "https://www.google.com"
            },
            "style": {
                "display": "=if([$Kent] == 'B', 'block', 'none')"
            }
        }
    ]
}
5
  • I have tried splitting them up in separate columns, button A works but button B doesnt show if i have an URL in "href" "=' ... ' " ´´´{ "$schema": "developer.microsoft.com/json-schemas/sp/v2/…", "elmType": "a", "txtContent": "NY", "attributes": { "target": "_blank", "href": "='google.dk'" } }´´´´ Commented Jul 22, 2020 at 13:12
  • if i strip www. and just use "href": "='google.dk'" the resulting link is "my sharepoint site/'google.dk' " Commented Jul 22, 2020 at 13:15
  • okay so figured out the HREF part, apparently i need to use a different syntax than everyone else. "href": "google.dk" works. Gonna rewrite the orignal code and see if thats whats been messing with it. Commented Jul 22, 2020 at 13:17
  • Yes, if you are hard coding the link then you don't need to use = in href (you can simply pass your link as a string). You need to use = only if you are adding any if condition or any other expression which is returning the link based on conditions. Commented Jul 22, 2020 at 13:34
  • However, Please Upvote(^) and accept as an Answer if it helped you in any way to solve this issue. Commented Jul 22, 2020 at 13:34
0

This works, while it creates either a button or a hyperlink, as buttons cant work as hyperlink. But the issue was the syntax of the "href" tag. All the googling i have done showed it to be written as "href": "=' URL '" which must have thrown some error which made the entire column blank. When i changed it to "href": "http://www.google.dk" it worked.

{
"elmType": "div",
"children": [
{
  "elmType": "button",
  "txtContent": "New ",
  "customRowAction": {
    "action": "executeFlow",
    "actionParams": "{\"id\": \"XXXXXXXXXXXXXXXXXXXXXXX\"}"
  },

"style":{
  "display": "=if(  ([$Kent] == 'A' ), 'block', 'none')"
}
},

{
  "elmType": "a",
  "txtContent": "New",
  "attributes": {
   "target": "_blank",
   "href": "https://www.google.dk"
},

"style":{
  "display": "=if(([$Kent] == 'B'), 'block', 'none')"
},
}
]
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.