參考文檔
語義說明
核心名詞
FetchXML:是基於Xml
的查詢語言,可以把它簡單理解成SQL
語句,通過它可以查詢Dynamics 365 CRM
的數據。開發人員可以在WebAPI
或者Organization Service
執行FetchXML
查詢來獲取數據,類似於SqlHelper.QueryTable(sql)
Action:Dynamics 365
流程中的一種,允許開發人員進行自定義開發,用來定制並組合各種業務邏輯,比如商機贏單、訂單提交。我們可以把它簡單理解成C#
中的一個方法,有輸入參數、輸出參數。操作的注冊模式分為兩種:一個是全局,一個是綁定到實體,可以獲取到實體ID
Web API:是客戶端連接服務端的一種方式,擁有良好的平台兼容性,不管什么平台都可以調用,與開發語言無關。它是基於OData v4.0
實現,提供了各種現代化的Restful Web
服務
Organization Service:是客戶端連接服務端的另外一種方式,它是基於WCF
技術實現,數據傳輸采用XML
,僅使用於.NET客戶端
Action
操縱類似與我們常用的方法,用於擴展系統的標准功能,用來實現項目中的業務邏輯。操作可以針對單個實體,也可以是全局(也就是任意實體都可使用)全局方法/局部方法
工作流中可以調用操作,JS也可以調用操作,通過后端C#代碼也可調用操作。始終在組織范圍內執行操作,不支持執行限制到用戶、業務部門或組織的范圍。
和Plugin
類似,都需要簽名,注冊到CRM中
自定義操作
新建Action流程
打開D365,進入我的流程中心
在流程中心頁面,點擊左上角新建按鈕,輸入流程名稱(全英文),類別選擇操作(Action),實體選擇無(全局)/實體(局部),建議全局,點擊確定創建
可選:定義輸入參數及輸出參數,完成后點擊保存,發布,激活
新建Action項目
打開VS,新建類庫項目(.NET 版本與D365版本對應)
新建類,繼承IPlugin
接口,並實現Execute
方法
using Microsoft.Xrm.Sdk;
using System;
namespace ActionApp
{
public class ConeAction : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(null); // 權限
Random rd = new Random();
string name = context.InputParameters["name"].ToString();
string area = context.InputParameters["area"].ToString();
string phone = context.InputParameters["phone"].ToString();
Entity entity = new Entity("new_customer");
entity["new_name"] = name + rd.Next(1000, 9999);
entity["new_area"] = area + rd.Next(1000, 9999);
entity["new_phone"] = phone + rd.Next(1000, 9999);
//entity["new_contact"] = entityRef;
entity["new_lasttime"] = DateTime.UtcNow.AddDays(rd.Next(1,100));
service.Create(entity);
context.OutputParameters["msg"] = "ok";
}
}
}
簽名:右鍵項目,屬性,簽名,勾選為程序集簽名,新建,填寫文件名稱,取消使用密碼保護密鑰文件,確定
編譯:右鍵項目,清理,重新生成
注冊/綁定Action
打開注冊工具,登錄,新建Assembly
在步驟一中選擇Action
項目編譯的DLL
文件
在步驟二中勾選添加(使用)的Action
(一個類庫可有多個action
),點擊選擇添加
新建執行時機:找的上一步添加的類庫集合,點擊展開,右鍵添加的Action,選擇添加Step
Message
:與action
流程名一致;Primary Entity
:關聯實體,若為實體則此Action
為局部(實體),none
為全局;其次,執行時期為PostOperation
,完成
使用Action
JS調用
// 調用全局帶參方法
function createEntity() {
var entity = new Object();
entity['name'] = '21586625'
entity['area'] = '21586625'
entity['phone'] = '21586625'
var req = new XMLHttpRequest();
req.open('post', Xrm.Page.context.getClientUrl() + "/api/data/v9.0/new_wyg_cone", false)
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) {
if (this.status == 200) alert(1)
}
}
req.send(JSON.stringify(entity));
}
調試Action
與插件(Plugin
)方式一致,詳情
接收參數
字符串類型
string name = context.InputParameters["name"].ToString();
string area = context.InputParameters["area"].ToString();
string phone = context.InputParameters["phone"].ToString();
整數類型
string name = context.InputParameters["name"].ToString();
int money = (int)context.InputParameters["money"];
int age = (int)context.InputParameters["age"];
布爾類型
string name = context.InputParameters["name"].ToString();
bool gender = (bool)context.InputParameters["gender"];
int age = (int)context.InputParameters["age"];
日期類型
string name = context.InputParameters["name"].ToString();
bool gender = (bool)context.InputParameters["gender"];
int age = (int)context.InputParameters["age"];
DateTime time = (DateTime)context.InputParameters["time"];
選擇列表
Entity(實體)類型
// Entity類型的參數建議指定@data.type (可以不指定,若是lookup到具體實體,不是customer,partylist類型),格式為Microsoft.Dynamics.CRM.實體邏輯名稱
// 需要指定記錄主鍵值,還有需要用到的該記錄的其他字段值
var cus = {}
cus['@data.type'] = 'Microsoft.Dynamics.CRM.new_customer'
cus.new_customerid = '1377FF4A-B494-E811-8ACD-005056948ABE'
cus.new_name = 'libai'
cus.new_area = '上海市'
entity['cus'] = cus
Entity cus = (Entity)context.InputParameters["cus"];
EntityReference(查找)類型
// EntityReference類型的參數指定記錄ID就可以,在流程參數類型中已執行實體
var cus = {}
cus.new_customerid = '56C134ED-A433-EB11-B392-005056993F73'
entity['cus'] = cus
EntityReference cus = (EntityReference)context.InputParameters["cus"];
// or
var s = context.InputParameters["cus"];
EntityCollection(實體列表)類型
var cusitem = {}
cus['@data.type'] = 'Microsoft.Dynamics.CRM.new_customer'
cus.new_customerid = '1377FF4A-B494-E811-8ACD-005056948ABE'
cus.new_name = 'libai'
cus.new_area = '上海市'
entity['cus'] = [cusitem]
EntityCollection cus = (EntityCollection)context.InputParameters["cus"];
// or
var s = context.InputParameters["cus"];
返回參數
context.OutputParameters["msg"] = "ok";