Dynamics CRM 用程式碼註冊Plugin Step

vic0228發表於2018-07-20

   平時我們開發時,外掛的註冊要麼通過工具,要麼通過SDK提供的VS元件,註冊完外掛還需要新增步驟等,那如果碰到根據業務需要靈活配置step時,再去通過工具新增或者刪除就不大友好了,最好能在介面上進行視覺化的操作。

   本篇要分享的場景是自動編號中,每增加一條實體配置時,點選頁面上的安裝就能自動建立外掛step,而不用手工去工具中新增

   下面開始上程式碼,在正式建立前會有一些程式碼塊來獲取一些必需的引數

   首先是獲取assembly的id,這是決定你要在哪個assembly下附加

 QueryExpression query = new QueryExpression("pluginassembly");
            query.ColumnSet = new ColumnSet("pluginassemblyid");
            query.Criteria.AddCondition("name", ConditionOperator.Equal, "NY_AutoNumber");
            EntityCollection collection = organizationServiceProxy.RetrieveMultiple(query);
            string assemblyid = collection.Entities[0].Id.ToString();

   然後是typeid,這個決定你的step掛在哪個plugin下面,這裡有個condition引數typename,初看之下可能並不知道是什麼,看下我紅框中框出來的和我程式碼中的名字對應下就看出來了

 //get plugin types  within assembly
            query = new QueryExpression("plugintype");
            query.ColumnSet = new ColumnSet("plugintypeid");//
            query.Criteria.AddCondition("pluginassemblyid", ConditionOperator.Equal, assemblyid);
            query.Criteria.AddCondition("typename", ConditionOperator.Equal, "NY_AutoNumber.CRM.Plugins.AutoNumber");
            collection = organizationServiceProxy.RetrieveMultiple(query);
            string pluginTypesId = collection.Entities[0].Id.ToString();

  

  然後是sdkmessageid,我這裡用的是Create, 可以根據業務靈活設定name

 //GetSdkMessageId
            query = new QueryExpression("sdkmessage");
            query.ColumnSet = new ColumnSet("sdkmessageid");
            query.Criteria.AddCondition("name", ConditionOperator.Equal, "Create");
            collection = organizationServiceProxy.RetrieveMultiple(query);
            string sdkmessageId = collection.Entities[0].Id.ToString();

  然後是sdkmessagefilterid

//GetSdkMessageFilterId
            query = new QueryExpression("sdkmessagefilter");
            query.ColumnSet = new ColumnSet(true);//"sdkmessagefilterid"
            query.Criteria.AddCondition("primaryobjecttypecode", ConditionOperator.Equal, entityName);
            query.Criteria.AddCondition("sdkmessageid", ConditionOperator.Equal, sdkmessageId);
            collection = organizationServiceProxy.RetrieveMultiple(query);
            string sdkmessagefilterId = collection.Entities[0].Id.ToString();

   到這裡準備工作就齊活了,然後就是根據引數來建立step了

Entity entity = new Entity("sdkmessageprocessingstep");
            entity.Attributes.Add("name", "NY_AutoNumber.CRM.Plugins.AutoNumber: Create of " + entityName);
            entity.Attributes.Add("description", "NY_AutoNumber.CRM.Plugins.AutoNumber: Create of " + entityName);
            entity.Attributes.Add("sdkmessagefilterid", new EntityReference("sdkmessagefilter", new Guid(sdkmessagefilterId)));
            entity.Attributes.Add("plugintypeid", new EntityReference("plugintype", new Guid(pluginTypesId)));
            entity.Attributes.Add("eventhandler", new EntityReference("plugintype", new Guid(pluginTypesId)));
            entity.Attributes.Add("componentstate", new OptionSetValue(0));
            entity.Attributes.Add("sdkmessageid", new EntityReference("sdkmessage", new Guid(sdkmessageId)));
            entity.Attributes.Add("asyncautodelete", false);
            entity.Attributes.Add("mode", new OptionSetValue(0));
            entity.Attributes.Add("stage", new OptionSetValue(20));
            entity.Attributes.Add("rank", 1);
            entity.Attributes.Add("supporteddeployment", new OptionSetValue(0));
            organizationServiceProxy.Create(entity);

 通過上述方式建立出來的step如下

相關文章