本人微信公众号:微软动态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.

JSON
{
 "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时各种类型输入参数传递值的方法的更多相关文章

  1. 如何让ASP.NET Web API的Action方法在希望的Culture下执行

    在今天编辑推荐的<Hello Web API系列教程--Web API与国际化>一文中,作者通过自定义的HttpMessageHandler的方式根据请求的Accep-Language报头 ...

  2. Only one complex type allowed as argument to a web api controller action.

    错误内容: message":"An error has occurred.","exceptionMessage":"Only one c ...

  3. ABP开发框架前后端开发系列---(10)Web API调用类的简化处理

    在较早期的随笔<ABP开发框架前后端开发系列---(5)Web API调用类在Winform项目中的使用>已经介绍了Web API调用类的封装处理,虽然这些调用类我们可以使用代码生成工具快 ...

  4. ABP开发框架前后端开发系列---(5)Web API调用类在Winform项目中的使用

    在前面几篇随笔介绍了我对ABP框架的改造,包括对ABP总体的介绍,以及对各个业务分层的简化,Web API 客户端封装层的设计,使得我们基于ABP框架的整体方案越来越清晰化, 也越来越接近实际的项目开 ...

  5. ABP开发框架前后端开发系列---(4)Web API调用类的封装和使用

    在前面随笔介绍ABP应用框架的项目组织情况,以及项目中领域层各个类代码组织,以及简化了ABP框架的各个层的内容,使得我们项目结构更加清晰.上篇随笔已经介绍了字典模块中应用服务层接口的实现情况,并且通过 ...

  6. 控制ASP.NET Web API 调用频率与限流

    ASP.NET MVC 实现 https://github.com/stefanprodan/MvcThrottle ASP.NET WEBAPI 实现 https://github.com/stef ...

  7. Web API对application/json内容类型的CORS支持

    假设有一简单架构分为前后两部分,其一是Angular构成的前端页面站点,另一个则是通过ASP.NET Web API搭建的后端服务站点.两个站点因为分别布署,所有会有CORS(Cross-Origin ...

  8. 搭建MVC及WEB API项目框架时碰到的问题集合

    前言 刚开始创建MVC与Web API的混合项目时,碰到好多问题,今天拿出来跟大家一起分享下.有朋友私信我问项目的分层及文件夹结构在我的第一篇博客中没说清楚,那么接下来我就准备从这些文件怎么分文件夹说 ...

  9. ASP.NET Web API 通过参数控制返回类型(JSON|XML)

    一个很实用的技巧,可以在访问web api服务的时候指定返回数据的格式类型,比如 json 或者 xml. 因为 web api 默认返回的是XML格式,但是现在json 比较流行,同时网上也有其他的 ...

随机推荐

  1. 1.1 What is the plug-in?

          A game center, such as Lianzhong in China, supports hundreds of games such as Chess, Bridges, ...

  2. SEO需要掌握的基础知识

    什么是SEO?  官方解释:  SEO是指通过对网站内部调整优化及站外优化,使网站满足搜索引擎收录排名需求,在搜索引擎中提高关键词排名, 从而把精准用户带到网站,获得免费流量,产生直接销售或品牌推广 ...

  3. [Swift]LeetCode497. 非重叠矩形中的随机点 | Random Point in Non-overlapping Rectangles

    Given a list of non-overlapping axis-aligned rectangles rects, write a function pick which randomly ...

  4. [Swift]LeetCode515. 在每个树行中找最大值 | Find Largest Value in Each Tree Row

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

  5. This relative module was not found:

    晚上项目敲完,关机睡觉!  早上醒来-----打开项目,惊呆了 !This relative module was not found: 如图   这个报错,我当时怎么也没看懂!!!   后来经过测试 ...

  6. Visual Studio 2017 怎么将自动生成属性设置为旧版格式

    工具:Visual Studio 2017 1.点击工具,进入选项 2.选项窗口左侧找到C#--代码样式,点击 3.找到表达式首选项中:使用属性的表达式主体.使用索引器的表达式主体和使用访问器的表达式 ...

  7. BBS论坛(十三)

    13.1点击更换图形验证码 (1)front/signup.html <div class="form-group"> <div class="inpu ...

  8. 智能压缩,摆脱用 Gzip 还是 Brotli 的纠结

    近日,又拍云上线了“智能压缩”功能,同时支持 Gzip 和 Brotli 压缩算法,在节约流量的同时,进一步减少用户的等待时间. CDN 流量问题一直以来是大家关注的重点,又拍云针对流量节约上线了一系 ...

  9. Dockerfile指令介绍

    FROM:指定基础镜像 在Dockerfile中FROM是必备的指令,用于指定基础的镜像. FROM centos:latest LABEL:指定镜像标签 LABEL指令用来指定镜像的标签. 格式: ...

  10. SQL注入: with rollup特性

    题目名称:因缺思汀的绕过 题目地址:http://www.shiyanbar.com/ctf/1940 1.with rollup: with rollup关键字会在所有记录的最后加上一条记录,该记录 ...