You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需Power App及微软应用:创建HTTPS表单并将数据导入Dataverse

不依赖Power App/微软平台创建HTTPS表单并同步数据到Dataverse的方案

核心思路是通过自定义HTML表单 + Dataverse Web API实现,完全不需要依赖Power App或微软低代码工具,只需利用Dataverse提供的REST接口完成数据写入。

步骤1:配置Dataverse权限与Azure AD应用

  • 确认你拥有目标Dataverse环境的访问权限,且对要写入的表(如自定义表、默认联系人表)具备Create/Write权限。
  • 在Azure AD中注册一个应用程序:
    • 记录下客户端ID、客户端密钥、租户ID。
    • 为该应用分配Dataverse的应用权限,选择Dynamics CRM > user_impersonation(或更精细的表级权限),并完成权限授予。

步骤2:搭建自定义HTTPS表单

编写纯HTML表单,前端处理用户输入并提交到你的后端服务(避免将敏感凭证暴露在前端)。示例表单代码:

<form id="dataForm">
  <div>
    <label>姓名:</label>
    <input type="text" id="firstName" required>
  </div>
  <div>
    <label>邮箱:</label>
    <input type="email" id="email" required>
  </div>
  <div>
    <label>电话:</label>
    <input type="tel" id="phone">
  </div>
  <button type="submit">提交</button>
</form>

<script>
document.getElementById('dataForm').addEventListener('submit', async (e) => {
  e.preventDefault();
  const formData = {
    firstName: document.getElementById('firstName').value,
    email: document.getElementById('email').value,
    phone: document.getElementById('phone').value
  };

  // 提交到自己的后端服务
  try {
    const response = await fetch('/submit-to-dataverse', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(formData)
    });
    if (response.ok) {
      alert('数据提交成功');
      document.getElementById('dataForm').reset();
    } else {
      alert('提交失败,请重试');
    }
  } catch (err) {
    console.error('请求错误:', err);
    alert('网络错误,请检查连接');
  }
});
</script>

步骤3:后端实现令牌获取与Dataverse API调用

以Node.js为例,编写后端接口中转表单数据并调用Dataverse API(避免前端暴露敏感凭证):

const express = require('express');
const fetch = require('node-fetch');
const app = express();
app.use(express.json());

// 配置Azure AD与Dataverse参数
const TENANT_ID = '你的租户ID';
const CLIENT_ID = '你的客户端ID';
const CLIENT_SECRET = '你的客户端密钥';
const DATAVERSE_ENV_URL = 'https://你的环境域名.crm.dynamics.com';
const API_VERSION = 'v9.2';

// 获取Dataverse访问令牌
async function getAccessToken() {
  const tokenEndpoint = `https://login.microsoftonline.com/${TENANT_ID}/oauth2/v2.0/token`;
  const params = new URLSearchParams();
  params.append('client_id', CLIENT_ID);
  params.append('client_secret', CLIENT_SECRET);
  params.append('grant_type', 'client_credentials');
  params.append('scope', `${DATAVERSE_ENV_URL}/.default`);

  const response = await fetch(tokenEndpoint, {
    method: 'POST',
    body: params
  });
  const data = await response.json();
  return data.access_token;
}

// 提交数据到Dataverse
app.post('/submit-to-dataverse', async (req, res) => {
  try {
    const token = await getAccessToken();
    // 这里以写入联系人表为例,替换为你的目标表逻辑名称
    const apiEndpoint = `${DATAVERSE_ENV_URL}/api/data/${API_VERSION}/contacts`;
    const payload = {
      firstname: req.body.firstName,
      emailaddress1: req.body.email,
      telephone1: req.body.phone
    };

    const response = await fetch(apiEndpoint, {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${token}`,
        'Content-Type': 'application/json',
        'OData-MaxVersion': '4.0',
        'OData-Version': '4.0'
      },
      body: JSON.stringify(payload)
    });

    if (response.ok) {
      res.status(201).json({ message: '数据写入成功' });
    } else {
      const errorData = await response.json();
      console.error('Dataverse API错误:', errorData);
      res.status(500).json({ message: '数据写入失败', error: errorData });
    }
  } catch (err) {
    console.error('后端处理错误:', err);
    res.status(500).json({ message: '服务器错误', error: err.message });
  }
});

// 启动HTTPS服务(生产环境需配置SSL证书)
const https = require('https');
const fs = require('fs');
const options = {
  key: fs.readFileSync('server.key'),
  cert: fs.readFileSync('server.cert')
};
https.createServer(options, app).listen(443, () => {
  console.log('HTTPS服务启动在443端口');
});

关键注意事项

  • 安全优先:绝对不要在前端代码中硬编码客户端密钥,必须通过后端中转请求,防止凭证泄露。
  • HTTPS要求:表单和后端服务必须部署在HTTPS环境下,否则无法调用Azure AD和Dataverse的API(浏览器会阻止混合内容请求)。
  • 字段映射:确保表单数据与Dataverse表的字段逻辑名称完全匹配(如联系人表的邮箱字段是emailaddress1,而非显示名称)。
  • 错误处理:在后端和前端都添加完善的错误捕获,便于排查令牌获取失败、API权限不足、字段格式错误等问题。

内容的提问来源于stack exchange,提问作者Wragnam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 20:40:29