如何通过API更新Microsoft Dynamics 365 Finance & Operations产品记录?求文档指引
Dynamics 365 Finance & Operations 产品记录更新API 开发指引
核心技术方向
- 使用D365官方标准的OData REST API,这是实现外部系统CRUD操作的原生方案,产品相关的OData实体集通常为
Products(未发布产品)或ReleasedProducts(已发布产品) - 必须通过Azure AD OAuth2.0完成身份验证,获取Bearer Token后才能发起合法请求
关键开发步骤
1. 获取Azure AD访问令牌(PHP示例)
首先需要在Azure AD中注册应用,获取租户ID、客户端ID、客户端密钥,然后发起令牌请求:
$tenantId = '你的租户ID'; $clientId = '你的客户端ID'; $clientSecret = '你的客户端密钥'; $d365InstanceUrl = 'https://你的D365实例域名'; // 例如 https://contoso.sandbox.operations.dynamics.com $tokenEndpoint = "https://login.microsoftonline.com/$tenantId/oauth2/token"; $postParams = [ 'grant_type' => 'client_credentials', 'client_id' => $clientId, 'client_secret' => $clientSecret, 'resource' => $d365InstanceUrl ]; $curl = curl_init($tokenEndpoint); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postParams)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $tokenResponse = curl_exec($curl); curl_close($curl); $tokenData = json_decode($tokenResponse, true); $accessToken = $tokenData['access_token'] ?? '';
2. 更新产品记录(PHP示例)
支持PUT(全量更新)和PATCH(增量更新)两种方式,推荐用PATCH减少数据传输:
$productRecId = '目标产品的RecId或外部ID'; // 例如 '00000001-0000-0000-0000-000000000001' $apiEndpoint = "$d365InstanceUrl/data/ReleasedProducts('$productRecId')"; // 已发布产品用ReleasedProducts,未发布替换为Products $updatePayload = [ 'Name' => '更新后的产品名称', 'ItemGroupId' => 'NEW_GROUP' // 新的产品组ID ]; $curl = curl_init($apiEndpoint); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH'); curl_setopt($curl, CURLOPT_HTTPHEADER, [ "Authorization: Bearer $accessToken", "Content-Type: application/json", "OData-MaxVersion: 4.0", "OData-Version: 4.0" ]); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($updatePayload)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curl); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if ($httpCode === 204) { echo '产品记录更新成功'; } else { echo "更新失败,响应:$response"; }
重要注意事项
- 确认实体名称:登录D365后,进入系统管理>数据实体,搜索产品相关实体,查看其OData集合名称
- 权限配置:Azure AD应用需要在D365中分配具备产品编辑权限的安全角色
- 字段名称:使用实体的技术字段名(而非界面显示名),可在数据实体的字段列表中查看
- 版本兼容:请求头中指定的OData版本需与D365实例兼容,通常使用4.0版本
内容的提问来源于stack exchange,提问作者EricP
相关产品推荐
相关产品推荐

