首页 文章

Powerapps DropDown控件问题:使用Azure函数中的text / records / list / DataRows填充下拉输入控件项

提问于
浏览
0

Agenda: 我们正在尝试在下拉列表中显示从SQL获取的数据,但不是直接给出我们正在使用"Azure Functions",它可以帮助通过密钥保险库从SQL检索数据

Issue: Powerapps DropDown / ListBox控件无法理解Azure函数给出的输出 . 我们尝试渲染Azure Functions的输出,即string / text / records / list / DataRows

Error:

属性需要表值,但此规则会生成不兼容的文本值

What we did:

  • 创建Azure功能(提供所有需要的访问权限,例如托管服务标识,应用程序添加到AAD等) . (测试函数,我们得到预期的输出)

  • 使用swagger 2.0创建API

  • 使用上述OpenAPI文件创建了自定义powerapp连接器(连接器工作正常)

  • 我们从Powerapp画布调用这个函数 MCFAzureFunction.RunThis(Button1.Pressed)

Help: 让我们知道如何最好地将响应数据fromazure功能推送到powerapp下拉列表或列表框控件 . 或者我们正在做/丢失一些东西

Code:

using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.KeyVault;
using Microsoft.Azure.Services.AppAuthentication;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;

namespace MCFuncApp
{
    public static class Function1
    {
        private static HttpClient client = new HttpClient();

        private static string BuildConnString(string secret, string dbCatalog)
        {
            return ("Server=********** Timeout=30;");
        }

        private static string DataTableToJSON(DataTable table)
        {
            string JSONString = string.Empty;
            JSONString = JsonConvert.SerializeObject(table);
            return JSONString;
        }

        [FunctionName("Function1")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {

            log.Info("C# HTTP trigger function processed a request.");

            // parse query parameter
            string name = req.GetQueryNameValuePairs()
                .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
                .Value;

            if (name == null)
            {
                // Get request body
                dynamic data = await req.Content.ReadAsAsync<object>();
                name = data?.name;
            }

            string exampleSecret = "Secret";
            string qResponse = "";

            if(name != null)
            {   // "https://********"
                string vaultBaseUrl = "*********";
                var azureServiceTokenProvider = new AzureServiceTokenProvider();
                var kvClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback), client);
                var secret = (await kvClient.GetSecretAsync(vaultBaseUrl)).Value;
                var connString = BuildConnString(secret, "*****");

                string query = "*********";

                var dt = new DataTable();
                using (SqlDataAdapter da = new SqlDataAdapter(query, connString))
                {
                    da.Fill(dt);
                }

                qResponse = DataTableToJSON(dt);                
            }


            return name == null
                ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
                : req.CreateResponse(HttpStatusCode.OK, qResponse, "application/json");
        }
    }
}

提前致谢 !

1 回答

  • 0

    我认为这个问题可能与 Azure Function 返回的 JSON 内容的结构有关 .

    受支持的结构应如下所示:

    Table({ Text: "Item 1", Val: 1 }, { Text: "Item 2", Val: 2 }, { Text: "Item 3", Val: 3 })
    

    您可能需要手动序列化返回的行,因为 DataRow 类包含的信息比您需要的多得多,并且在将其作为响应的内容传回之前使用 StringBuilder 连接最终结果 .

    希望能帮助到你 .

相关问题