Dynamics CRM 2015/2016新特性之二十四:使用Web API执行操作
关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复207或者20160316可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!
操作(action)是可重复利用的操作,操作会改变数据,可以调用的系统自带的操作列表请参考SDK的 Web API Action Reference 章节,当然自己建立的操作(流程的一种)也是可以像系统自带的操作那样可以通过Web API调用。
操作也分绑定操作(bound action)和非绑定操作(unbound action),也是根据操作的元数据是否有IsBound="true"。这里以调用 WinOpportunity 这个非绑定操作为例来查看和调用系统自带的操作。
示例代码:
var clientURL = Xrm.Page.context.getClientUrl(); var req = new XMLHttpRequest() req.open("POST", encodeURI(clientURL + "/api/data/v8.0/WinOpportunity"), true); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.onreadystatechange = function () { if (this.readyState == 4 /* complete */) { req.onreadystatechange = null; if (this.status == 204) { Xrm.Utility.alertDialog("将商机作为赢单关闭成功!"); } else { var error = JSON.parse(this.response).error; Xrm.Utility.alertDialog("将商机作为赢单关闭出错." + error.message); } } }; var requestmsg = {}; requestmsg.Status = 3; requestmsg.OpportunityClose = {}; requestmsg.OpportunityClose.subject = "罗勇结束了这个商机"; requestmsg.OpportunityClose[""] = "/opportunities(6025165A-3AA3-E511-80C7-000D3A807EC7)"; req.send(JSON.stringify(requestmsg));
下面我们来看看调用绑定操作(bound action),我们拿 RemoveMembersTeam 这个操作来演示,先看看它的定义:
然后下面是我撰写的示例代码,注意调用绑定操作的话,第一个参数是在调用的URL里面指定的,而且操作的名称的前面要加上命名空间Microsoft.Dynamics.CRM,第二个参数如果是某个类型的Collection,则是一个数组,这个类型则是指定其主键的值即可,这是我猜测的,适用于当前的例子。
var clientURL = Xrm.Page.context.getClientUrl(); var req = new XMLHttpRequest() req.open("POST", encodeURI(clientURL + "/api/data/v8.0/teams(E4CC382D-02B9-E511-80DC-000D3A804C3F)/Microsoft.Dynamics.CRM.RemoveMembersTeam"), true); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.onreadystatechange = function () { if (this.readyState == 4 /* complete */) { req.onreadystatechange = null; if (this.status == 204) { Xrm.Utility.alertDialog("将用户移出团队成功!"); } else { var error = JSON.parse(this.response).error; Xrm.Utility.alertDialog("将用户移出团队出错." + error.message); } } }; var requestmsg = {}; requestmsg.Members = []; requestmsg.Members[0] = {}; requestmsg.Members[0].systemuserid = "A576C4B5-44A9-E511-80CF-000D3A806074"; requestmsg.Members[1] = {}; requestmsg.Members[1].systemuserid = "CEFE67E5-44A9-E511-80CF-000D3A806074"; req.send(JSON.stringify(requestmsg));
下面我模仿SDK上面的说明配置一个操作如下,并用代码来调用它。
发布这个操作以后,我们去元数据页面(比如我的是 http://lycrmvm.cloudapp.net:5555/Demo/api/data/v8.0/$metadata ,需要激活操作并且发布,然后刷新页面哦)可以搜索到如下的操作定义:
然后调用代码如下:
var clientURL = Xrm.Page.context.getClientUrl(); var req = new XMLHttpRequest() req.open("POST", encodeURI(clientURL + "/api/data/v8.0/ly_tests(A9B987FB-A4A4-E511-80CC-000D3A80CE7F)/Microsoft.Dynamics.CRM.ly_AddNoteToContact"), true); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.onreadystatechange = function () { if (this.readyState == 4 /* complete */) { req.onreadystatechange = null; if (this.status == 200) { var responseJSON = JSON.parse(this.responseText); Xrm.Utility.alertDialog("注释创建成功,注释ID是" + responseJSON.annotationid); } else { var error = JSON.parse(this.response).error; Xrm.Utility.alertDialog("创建注释出错." + error.message); } } }; var requestmsg = {}; requestmsg.NoteTitle = "这是罗勇通过WEB API创建的注释标题"; requestmsg.NoteText = "这是罗勇通过WEB API创建的注释内容"; req.send(JSON.stringify(requestmsg));
从Dynamics 365 V9版本开始提供了新的客户端API来方便编程,请参考 Xrm.WebApi.online.execute (Client API reference) ,还可以参考我的博文 Dynamics 365 V9版本新的客户端API Xrm.WebApi.online.execute 使用实例 。
更多信息请参考官方文档:Use Web API actions 。
相关推荐
染血白衣 2020-11-16
SAMXIE 2020-11-04
一个智障 2020-11-15
学习web前端 2020-11-09
yiranpiaoluo 2020-11-04
lxhuang 2020-11-03
88274956 2020-11-03
82387067 2020-11-03
huangliuyu00 2020-10-29
sichenglain 2020-10-27
Dayer 2020-10-27
小马的学习笔记 2020-10-23
liuweiITlove 2020-10-14
kjyiyi 2020-10-10
fanjunjaden 2020-10-09
zhyue 2020-09-28
huangliuyu00 2020-09-24
88397813 2020-09-23
jyj0 2020-09-21