平时我们开发时,插件的注册要么通过工具,要么通过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如下