通过Web API调用Action时各种类型输入参数传递值的方法
本人微信公众号:微软动态CRM专家罗勇 ,回复280或者20180906可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me 。
Dynamics 365的Web API功能很强大也越来越强大,流程的一种操作(Action)的功能也很强大也越来越强大(有个特征很好用,就是这些步骤默认组成一个事物,要么都成功,要么都失败)。
工作流中可以调用操作,通过Web API也可以方便的调用。
最近操作的参数增加了Entity这种类型,调用的时候如何赋值呢?我下面这个操作有两个复杂点的输入参数,一个类型为EntityReference的输入参数,一个类型为Entity的输入参数,还有一个类型为EntityReference的输出参数。

为啥要用到Entity类型的参数?典型场景就是我的步骤中需要用到这个Entity记录的非主键,非主属性字段的值,操作中某些字段赋值要用同类型,所以这时候就用上了。
为啥要用EntityReference类型的参数?给某个查找类型的字段赋值就要用到这种参数。
输出参数为EntityReference啥时候用到?你要获取到某个记录的ID时候,比如操作中创建的记录的ID。
官方有篇文章 Use Web API actions 介绍了Entity类型参数的赋值,可以参考,我这里汇总下调用方法如下:
var clientURL = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest()
req.open("POST", encodeURI(clientURL + "/api/data/v8.2/new_lytests(93EDAC5A-ACA6-E811-8ACE-005056948ABE)/Microsoft.Dynamics.CRM.new_TestbyLuoYong"), 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) {
//注意EntityReference类型输出参数的值是记录的主键值
Xrm.Utility.alertDialog(JSON.parse(this.response).new_workorderid);
}
else {
var error = JSON.parse(this.response).error;
Xrm.Utility.alertDialog("Error." + error.message);
}
}
};
var requestmsg = {};
//Entity类型的参数建议指定@data.type (可以不指定,若是lookup到具体实体,不是customer,partylist类型),格式为Microsoft.Dynamics.CRM.实体逻辑名称
//需要指定记录主键值,还有需要用到的该记录的其他字段值
requestmsg.WorkOrderType = {};
requestmsg.WorkOrderType.new_subjectid='1377FF4A-B494-E811-8ACD-005056948ABE';
requestmsg.WorkOrderType["@data.type"]="Microsoft.Dynamics.CRM.new_subject";
requestmsg.WorkOrderType.new_optionsetfielvalue = 1;
//EntityReference类型的参数指定记录ID就可以
requestmsg.ASP={};
requestmsg.ASP.accountid='FC96BE10-63AF-E811-8ACE-005056948ABE';
req.send(JSON.stringify(requestmsg));
对于参数类型为EntityCollection 类型参数的赋值,可以参考文章 Execute Action with “EntityCollection” Parameter using Web API in Dynamics 365 ,其实就是一个Entity类型参数的数组嘛。
我这里也摘录下官方的原文供各位参考:
When an action requires an entity as a parameter and the type of entity is ambiguous, you must use the @odata.type property to specify the type of entity. The value of this property is the fully qualified name of the entity, which follows this pattern: Microsoft.Dynamics.CRM.+<entity logical name>.
As shown in the Bound actions section above, the Target parameter to the AddToQueue Action is an activity. But since all activities inherit from the activitypointer EntityType, you must include the following property in the entity JSON to specify the type of entity is a letter: "@odata.type": "Microsoft.Dynamics.CRM.letter".
Two other examples are AddMembersTeam Action and RemoveMembersTeam Action because the Members parameter is a collection of systemuser EntityType which inherits it's ownerid primary key from the principal EntityType. If you pass the following JSON to represent a single systemuser in the collection, it is clear that the entity is a systemuser and not a team EntityType, which also inherits from the principal entitytype.
{ "Members": [{
"@odata.type": "Microsoft.Dynamics.CRM.systemuser",
"ownerid": "5dbf5efc-4507-e611-80de-5065f38a7b01"
}]
}
If you do not specify the type of entity in this situation, you can get the following error: "EdmEntityObject passed should have the key property value set.".
通过Web API调用Action时各种类型输入参数传递值的方法的更多相关文章
- 如何让ASP.NET Web API的Action方法在希望的Culture下执行
在今天编辑推荐的<Hello Web API系列教程--Web API与国际化>一文中,作者通过自定义的HttpMessageHandler的方式根据请求的Accep-Language报头 ...
- Only one complex type allowed as argument to a web api controller action.
错误内容: message":"An error has occurred.","exceptionMessage":"Only one c ...
- ABP开发框架前后端开发系列---(10)Web API调用类的简化处理
在较早期的随笔<ABP开发框架前后端开发系列---(5)Web API调用类在Winform项目中的使用>已经介绍了Web API调用类的封装处理,虽然这些调用类我们可以使用代码生成工具快 ...
- ABP开发框架前后端开发系列---(5)Web API调用类在Winform项目中的使用
在前面几篇随笔介绍了我对ABP框架的改造,包括对ABP总体的介绍,以及对各个业务分层的简化,Web API 客户端封装层的设计,使得我们基于ABP框架的整体方案越来越清晰化, 也越来越接近实际的项目开 ...
- ABP开发框架前后端开发系列---(4)Web API调用类的封装和使用
在前面随笔介绍ABP应用框架的项目组织情况,以及项目中领域层各个类代码组织,以及简化了ABP框架的各个层的内容,使得我们项目结构更加清晰.上篇随笔已经介绍了字典模块中应用服务层接口的实现情况,并且通过 ...
- 控制ASP.NET Web API 调用频率与限流
ASP.NET MVC 实现 https://github.com/stefanprodan/MvcThrottle ASP.NET WEBAPI 实现 https://github.com/stef ...
- Web API对application/json内容类型的CORS支持
假设有一简单架构分为前后两部分,其一是Angular构成的前端页面站点,另一个则是通过ASP.NET Web API搭建的后端服务站点.两个站点因为分别布署,所有会有CORS(Cross-Origin ...
- 搭建MVC及WEB API项目框架时碰到的问题集合
前言 刚开始创建MVC与Web API的混合项目时,碰到好多问题,今天拿出来跟大家一起分享下.有朋友私信我问项目的分层及文件夹结构在我的第一篇博客中没说清楚,那么接下来我就准备从这些文件怎么分文件夹说 ...
- ASP.NET Web API 通过参数控制返回类型(JSON|XML)
一个很实用的技巧,可以在访问web api服务的时候指定返回数据的格式类型,比如 json 或者 xml. 因为 web api 默认返回的是XML格式,但是现在json 比较流行,同时网上也有其他的 ...
随机推荐
- Java作业十(2017-11-8)
public class TAutoPerson { public static void main(String args[]) { new TAutoPerson().fun(); } publi ...
- 一个自己研究出来的字符串匹配算法-k子串算法
前言 最近工作中需要写一个算法,而写完这个算法我却发现了一个很有意思的事情.需要的这个算法是这样的:对于A,B两个字符串,找出最多K个公共子串,使得这K个子串长度和最大.百度之没有这样的算法,然后就开 ...
- 【RL-TCPnet网络教程】第32章 RL-TCPnet之Telnet服务器
第32章 RL-TCPnet之Telnet服务器 本章节为大家讲解RL-TCPnet的Telnet应用,学习本章节前,务必要优先学习第31章的Telnet基础知识.有了这些基础知识之后,再搞 ...
- 【安富莱专题教程第1期】基于STM32的硬件RGB888接口实现emWin的快速刷新方案,32位色或24
说明:1. 首先感谢ST终于推出了ARGB格式的emWin库,可谓千呼万唤始出来,使用STM32的硬件RGB888接口刷新图片慢的问题终于得到解决.2. 这个问题由来已久,是之前为我们的STM32-V ...
- PHP workerMan tcp与webSocket 透传互通
<?php $work_path = dirname(__FILE__); chdir($work_path); use \Workerman\Worker; use \Workerman\Li ...
- Oracle数据库备份及还原
Oracle数据库备份 1:找到Oracle安装路径我的就是默认C盘 C:\app\wdjqc\admin\orcl\adump 2:执行文件:back.bat 文件内容如下: @echo off ...
- [Swift]LeetCode281. 之字形迭代器 $ Zigzag Iterator
Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...
- Java中构造方法、实例方法、类方法的区别
1. 构造方法 构造方法负责对象的初始化工作,为实例变量赋予合适的初始值.必须满足以下的语法规则: 方法名与类名相同: 不要返回类型(例如return.void等): 不能被static.final. ...
- python网络-多进程(21)
一.什么是进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实 ...
- mac openresty 源码安装 坑
下载openresty源码安装 下载页面http://openresty.org/cn/download.html 下载上一个版本的稳定版 https://openresty.org/download ...