How do dynamically bind data to adaptive card?

喜夏-厌秋 提交于 2020-04-30 06:38:12

问题


I have created a bot using c#.net. I am fetching data from the SharePoint list and it will fetch a number of rows. The number of rows is dynamic. I want to add data in a Table format when the bot will answer in Teams. I want to use adaptive cards for showing data in a table format, but how do I bind that data in an adaptive card as the number of rows will change according to question asked to bot? How can I bind data to cad dynamically in c#.net bot code?


回答1:


This is no problem at all, you can simply build up the adaptive cards server-side (i.e. in your bot) with the amount of rows. It's possible to either construct the JSON for the adaptive card as a string (using stringbuilder) or, as you're using C#, you can using the strongly typed AdaptiveCards nuget package and do it all in C#, something like:

            var items = new System.Collections.Generic.List<AdaptiveElement>();
            for (int i = 0; i < whatever.count; i++)
            {
                items.Add(new AdaptiveTextBlock()
                {
                    Text = $"Item {i}",
                });
            }

            card.Body.Add(
                new AdaptiveContainer()
                {
                    Items = items
                }
            );

I discuss this more in this answer: How to convert custom json to adaptive card json format



来源:https://stackoverflow.com/questions/59943893/how-do-dynamically-bind-data-to-adaptive-card

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!