今天想實現一個Search Product的功能,首先要將數據展示在頁面,然后前端根據查詢需求進行處理。之前是在salesforce中實現的,可以定義一個Search Product的頁面,然后在頁面中訪問查詢數據的Webservice即可。但是在Dynamic 365中并沒有這種直接調用的方式,最終找到一種方式就是,前端頁面通過Js調用工作流中的Action,而在Action上綁定了插件。
實現效果
(1) 實現html頁面并導入系統
在設置中找到自定義項,然后進去自定義系統,選擇Web資源,然后將之前做好的頁面上傳到系統。
(2) 自定義Action
首先新建一個action,實體設置上可以設置為全局的,也可以單獨設定某個實體。
注意:
注冊該action,方法同插件注冊,在注冊step時message選擇我們的action的唯一名稱,很多人在這一步的message里不顯示action的名字,確保兩點:第一你的action激活了,第二你的插件注冊器是在你激活action后再打開的。
設置好action,這邊設置了兩個輸入參數和一個輸出參數,設置完后保存并且激活。
(3) 開發Plugin項目
這里我們簡單介紹一下插件的基本用法
1. 要繼承IPlugin,并實現Excute方法。
2. 從Service provider里獲取執行上下文
3. 我們可以檢查出發插件的實體名稱
4. 還可以檢查觸發的事件,是creat, update還是delete
5. 輸入參數里獲取觸發的實體
6. 通過service factory獲取IOrganizationService,當CreateOrganizationService方法的參數為null時,表示的是系統用戶,當參數為context.UserId 或Guid.Empty時,表示的是當前用戶
7. 最后是DoAction方法,插件的邏輯就可以在這里實現了。
以下是實現插件的代碼:
public void Execute(IServiceProvider serviceProvider){IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);//傳入兩個參數String InParameters1 = context.InputParameters["InParameters1"] as String;String InParameters2 = context.InputParameters["InParameters2"] as String;//查詢//EntityReference entityRef = context.InputParameters["Target"] as EntityReference;string fetchProductXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'><entity name='new_be_eligible_pn__c'><attribute name='new_be_eligible_pn__cid' /><attribute name='new_name' /><attribute name='createdon' /><attribute name='new_value_category__c' /><attribute name='new_is_tp_part__c' /><attribute name='new_country__c' /><attribute name='new_available_quantity_flag__c' /><order attribute='new_name' descending='false' /><filter type='and'><condition attribute='new_country__c' operator='eq' value='Italy' /></filter><link-entity name='new_product' from='new_productid' to='new_product__c' link-type='inner' alias='a_e79f28ae2899e811a96f000d3a828e6c'><attribute name='new_wifi__c' /><attribute name='new_warranty__c' /><attribute name='new_type__c' /><attribute name='new_sub_series__c' /><attribute name='new_series__c' /><attribute name='new_screen_size__c' /><attribute name='new_name' /><attribute name='new_product_hierarchy__c' /><attribute name='new_description' /><attribute name='new_productcode' /><attribute name='new_os__c' /><attribute name='new_memory__c' /><attribute name='new_hdd__c' /><attribute name='new_cpu__c' /><attribute name='new_brand__c' /><attribute name='new_battery__c' /><filter type='and'><filter type='or'><condition attribute='new_type__c' operator='like' value='%PC System Unit%' /><condition attribute='new_type__c' operator='eq' value='Non System Unit' /></filter></filter></link-entity><link-entity name='new_product_sales_country__c' from='new_product_sales_country__cid' to='new_psc__c' link-type='inner' alias='a_c278acd92899e811a96f000d3a828e6c'><attribute name='new_currencyisocode' /><attribute name='new_channel_price__c' /><filter type='and'><condition attribute='new_sales_status__c' operator='like' value='%12%' /></filter></link-entity></entity></fetch>";EntityCollection products = service.RetrieveMultiple(new FetchExpression(fetchProductXml));//傳出參數context.OutputParameters["OutParameters"] = products;//遍歷productsforeach (var pair in products.Entities){foreach (var pa in pair.Attributes){Console.WriteLine(pa.Key + ": " + pa.Value);}}Console.WriteLine(products);}
(4)數據查詢方式,本篇使用FetchXML
在以上的代碼實現中,我使用了FetchXML的方式實現,對于多條件跨表查詢,返回多條記錄很方便。常用的結構如下,
SDK中有很多類似的案例。
其中XML這塊我們可以使用高級查詢來自動生成
最后我們可以導出XML文件
(5)Js調用自定義的Action
最后可以看到輸出結果:
總結:
在on-premises的開發中我們往往把復雜的業務處理邏輯封裝成接口供前端js調用,但在online的開發中這樣的開發方式無疑增加了成本(需另外架設服務器,添置域名,配置https協議證書等),通過web api調用action給我們提供了新的可行的方式。實現了需求。在下一章我們將分享如何調試插件。
總結
以上是生活随笔為你收集整理的【转】Dynamics CRM 365零基础入门学习(三)Dynamics 通过Web API 来调用自定义的Action(使用插件)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。