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

如何调用外部API获取Excel文件并附加到CRM的Note记录时间线?

把API生成的Excel文件上传到Dynamics 365备注(时间线)的实现方案

你已经通过fetch拿到了API返回的Excel Blob文件,接下来只需要完成Blob转Base64和调用Web API创建备注记录这两步,就能把文件附加到对应Quote的时间线里。

核心步骤说明

  • Blob转Base64:Dynamics 365的备注(annotation实体)要求文件内容以Base64字符串存在documentbody字段里,所以需要先把获取到的Blob转成这种格式。
  • 调用Web API创建备注:通过Dynamics 365的Web API提交包含文件信息的请求,关联到目标Quote记录,文件就会自动出现在该记录的时间线中。

修改后的完整代码

function sendQuoteWithAPI(quoteId, selectedItems) {
    var tokenId = 'tokenid';
    ISV_abs_CloseDialog('ok');
    var confirmStrings = {
        text: "Excel文件生成中。\n 文件将上传至时间线。\n\n\n 如需取消请点击取消按钮。",
        title: "导出至Excel确认!", confirmButtonLabel: "确认"
    };
    var confirmOptions = { height: 200, width: 400 };
    var endpointURL = 'https://dynamics365-excelexport-dev.azurewebsites.net/api/export-quote?quoteId='+quoteId+'&customer='+selectedItems;
    var requestOptions = {
        method: 'GET',
        headers: {
            'Token': tokenId
        }};

    fetch(endpointURL, requestOptions)
        .then(response => response.blob())
        .then(blob => {
            // 把Blob转成纯Base64字符串(去掉dataURL前缀)
            return new Promise((resolve, reject) => {
                const reader = new FileReader();
                reader.onloadend = () => resolve(reader.result.split(',')[1]);
                reader.onerror = reject;
                reader.readAsDataURL(blob);
            });
        })
        .then(base64Content => {
            // 构造备注记录数据,提交给Web API
            const noteData = {
                "subject": "报价导出Excel",
                "filename": "报价详情.xlsx",
                "documentbody": base64Content,
                "mimetype": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                "objectid@odata.bind": "/quotes(" + quoteId + ")",
                "objecttypecode": "quote"
            };

            return window.parent.Xrm.WebApi.createRecord("annotation", noteData);
        })
        .then(() => {
            // 上传成功提示
            window.parent.Xrm.Navigation.openAlertDialog({ text: "Excel文件已成功上传至时间线!" }, { height: 150, width: 300 });
        })
        .catch(error => {
            console.log('错误:', error);
            window.parent.Xrm.Navigation.openAlertDialog({ text: "文件上传失败:" + error.message }, { height: 150, width: 300 });
        });
}

关键细节提示

  • Base64转换:FileReader.readAsDataURL返回的字符串带data:[mimetype];base64,前缀,必须截取逗号后的部分才能符合CRM要求。
  • 关联目标记录:objectid@odata.bind字段格式固定为/实体集合名(记录ID),Quote的集合名是quotes,如果是其他实体需要对应修改。
  • MIME类型:xlsx格式用application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,老版xls格式用application/vnd.ms-excel。
  • 权限要求:当前用户需要具备创建annotation记录的权限,以及访问目标Quote记录的权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 23:34:55