-2

I have an adaptive card json with Action.ShowCard property and 1 submit button. I want to convert it to C# object and loop the Action.showcard to dynamically build multiple showcard buttons based on the count/data coming from an api call.

I was able to convert it to c# object using "AdaptiveCard.FromJson(json).Card", but not able to proceed further

Suppose i get 3 project codes, so i want to build 3 action.showcards and populate its fields from api data.

Below is the adaptive card with only 1 showcard, I need to consume this card and build multiple showcards.

{
"type": "AdaptiveCard",
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.3",
"body": [
    {
        "type": "TextBlock",
        "text": "**Timesheet Project Codes**",
        "wrap": true
    },
    {
        "type": "ActionSet",
        "actions": [
            {
                "type": "Action.ShowCard",
                "title": "123456",
                "card": {
                    "type": "AdaptiveCard",
                    "body": [
                        {
                            "type": "ColumnSet",
                            "columns": [
                                {
                                    "type": "Column",
                                    "width": "stretch",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "text": "Project Code:",
                                            "wrap": true
                                        }
                                    ]
                                },
                                {
                                    "type": "Column",
                                    "width": "stretch",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "text": "123456",
                                            "wrap": true
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "type": "ColumnSet",
                            "columns": [
                                {
                                    "type": "Column",
                                    "width": "stretch",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "text": "Project Name:",
                                            "wrap": true
                                        }
                                    ]
                                },
                                {
                                    "type": "Column",
                                    "width": "stretch",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "text": "abc/def/ghi",
                                            "wrap": true
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "type": "ColumnSet",
                            "columns": [
                                {
                                    "type": "Column",
                                    "width": "stretch",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "text": "Cost Code:",
                                            "wrap": true
                                        }
                                    ]
                                },
                                {
                                    "type": "Column",
                                    "width": "stretch",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "text": "ghi1234",
                                            "wrap": true
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "type": "ColumnSet",
                            "columns": [
                                {
                                    "type": "Column",
                                    "width": "stretch",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "text": "Project Code Start Date:",
                                            "wrap": true
                                        }
                                    ]
                                },
                                {
                                    "type": "Column",
                                    "width": "stretch",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "text": "January 1, 2022",
                                            "wrap": true
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "type": "ColumnSet",
                            "columns": [
                                {
                                    "type": "Column",
                                    "width": "stretch",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "text": "Project Code End Date: ",
                                            "wrap": true
                                        }
                                    ]
                                },
                                {
                                    "type": "Column",
                                    "width": "stretch",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "text": "December 31, 2025",
                                            "wrap": true
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "type": "ColumnSet",
                            "columns": [
                                {
                                    "type": "Column",
                                    "width": "stretch",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "text": "Project Code Allocation Start Date:",
                                            "wrap": true
                                        }
                                    ]
                                },
                                {
                                    "type": "Column",
                                    "width": "stretch",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "text": "March 1, 2022",
                                            "wrap": true
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "type": "ColumnSet",
                            "columns": [
                                {
                                    "type": "Column",
                                    "width": "stretch",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "text": "Project Code Allocation End Date:",
                                            "wrap": true
                                        }
                                    ]
                                },
                                {
                                    "type": "Column",
                                    "width": "stretch",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "text": "November 31, 2022",
                                            "wrap": true
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            }
        ]
    },
    {
        "type": "ActionSet",
        "actions": [
            {
                "type": "Action.Submit",
                "title": "Fill Timesheet",
                "data": "Fill Timesheet"
            }
        ]
    }
]

}

SDR
  • 37
  • 1
  • 1
  • 8
  • 1
    Are you having problems in de-serializing the above `JSON` output? – Rahul Sharma Mar 25 '22 at 14:34
  • @RahulSharma No i can de-serialize. The problem is that after de-serializing i want to dynamically build the Adaptive show card. So here there is only 1 show card. But i want to create multiple showcards from C# code, each having different contents. – SDR Mar 25 '22 at 14:43
  • 1
    Why not create a proper `Model` for the above `JSON` and then de-serialize it to a `List` ? This will get all the content. If you can post your entire `JSON` output string, then we can go from there. – Rahul Sharma Mar 25 '22 at 14:48

1 Answers1

1

From your explanation, I understand that you're using AdaptiveCard.FromJson to deserialize the entire thing into onc card.

Instead, you could try deserializing the JSON and then parsing it to build multiple separate Adaptive Cards in code.

Once you have built C# objects from your parsed JSON, you can then manipulate them and add or remove components as desired.

AP01
  • 820
  • 1
  • 2
  • 8