今天想實現(xiàn)一個Search Product的功能,首先要將數(shù)據(jù)展示在頁面,然后前端根據(jù)查詢需求進行處理。之前是在salesforce中實現(xiàn)的,可以定義一個Search Product的頁面,然后在頁面中訪問查詢數(shù)據(jù)的Webservice即可。但是在Dynamic 365中并沒有這種直接調(diào)用的方式,最終找到一種方式就是,前端頁面通過Js調(diào)用工作流中的Action,而在Action上綁定了插件。
實現(xiàn)效果
(1) 實現(xiàn)html頁面并導(dǎo)入系統(tǒng)
在設(shè)置中找到自定義項,然后進去自定義系統(tǒng),選擇Web資源,然后將之前做好的頁面上傳到系統(tǒng)。
(2) 自定義Action
首先新建一個action,實體設(shè)置上可以設(shè)置為全局的,也可以單獨設(shè)定某個實體。
注意:
注冊該action,方法同插件注冊,在注冊step時message選擇我們的action的唯一名稱,很多人在這一步的message里不顯示action的名字,確保兩點:第一你的action激活了,第二你的插件注冊器是在你激活action后再打開的。
設(shè)置好action,這邊設(shè)置了兩個輸入?yún)?shù)和一個輸出參數(shù),設(shè)置完后保存并且激活。
(3) 開發(fā)Plugin項目
這里我們簡單介紹一下插件的基本用法
1. 要繼承IPlugin,并實現(xiàn)Excute方法。
2. 從Service provider里獲取執(zhí)行上下文
3. 我們可以檢查出發(fā)插件的實體名稱
4. 還可以檢查觸發(fā)的事件,是creat, update還是delete
5. 輸入?yún)?shù)里獲取觸發(fā)的實體
6. 通過service factory獲取IOrganizationService,當(dāng)CreateOrganizationService方法的參數(shù)為null時,表示的是系統(tǒng)用戶,當(dāng)參數(shù)為context.UserId 或Guid.Empty時,表示的是當(dāng)前用戶
7. 最后是DoAction方法,插件的邏輯就可以在這里實現(xiàn)了。
以下是實現(xiàn)插件的代碼:
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);//傳入兩個參數(shù)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));//傳出參數(shù)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)數(shù)據(jù)查詢方式,本篇使用FetchXML
在以上的代碼實現(xiàn)中,我使用了FetchXML的方式實現(xiàn),對于多條件跨表查詢,返回多條記錄很方便。常用的結(jié)構(gòu)如下,
SDK中有很多類似的案例。
其中XML這塊我們可以使用高級查詢來自動生成
最后我們可以導(dǎo)出XML文件
(5)Js調(diào)用自定義的Action
最后可以看到輸出結(jié)果:
總結(jié):
在on-premises的開發(fā)中我們往往把復(fù)雜的業(yè)務(wù)處理邏輯封裝成接口供前端js調(diào)用,但在online的開發(fā)中這樣的開發(fā)方式無疑增加了成本(需另外架設(shè)服務(wù)器,添置域名,配置https協(xié)議證書等),通過web api調(diào)用action給我們提供了新的可行的方式。實現(xiàn)了需求。在下一章我們將分享如何調(diào)試插件。
總結(jié)
以上是生活随笔為你收集整理的【转】Dynamics CRM 365零基础入门学习(三)Dynamics 通过Web API 来调用自定义的Action(使用插件)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。