Dynamics CRM - Plug-in Class 和 Workflow Class 的用法与区别
在 Dynamics CRM 开发中,我们可以使用 JavaScript 在前端对 Entity Form 进行数据操作,与此同时,我们也可以使用 C# 写后台插件,其中就包括了 Plug-in Class 和 Workflow Class,如下图所示,这里也简单阐述下两者在使用上的区别:

图1 Plug-in Class 和 Workflow Class
一、调用范围:
Plug-in Class 是在对 Entity 的创建(Create)和更新(Update)时进行调用,而 Workflow Class 是在 Business Process Flow (BPF) 的 workflow 组件里进行调用。
二、使用方式:
Plug-in Class:
编写程序 -> 编译程序集,得到 dll -> 打开 PluginRegistration.exe(路径:SDK 安装目录 > Tools > PluginRegistration)-> 连接 CRM Server -> 选择要部署的 Organization -> 注册 Assembly -> 注册 Step(Create/Update -> Pre-operation/Post-operation) -> 注册 Image(可选,PreImage/PostImage)
完成以上注册后就可以在 CRM 上被调用了。

图2.1 注册 Plug-in 程序集
Workflow Class:
编写程序 -> 编译程序集,得到 dll -> 打开 PluginRegistration.exe -> 连接 CRM Server -> 选择要部署的 Organization -> 注册 Assembly
在注册完 Workflow 程序集后,将 dll 包装在 workflow 组件中,提供给 BPF 调用。

图2.2 注册 Workflow 程序集

图2.3 创建 workflow 组件

图2.4 workflow 组件与 BPF

图2.5 在 BPF 中使用 workflow 组件
三、获取 Entity 对象的方式:
Plug-in Class:
Entity entity = (Entity)context.InputParameters["Target"];
Workflow Class:
Entity entity = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
四、代码示例:
Plug-in Class:
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System; namespace CRMPluginProject_Test
{
public class PluginClass2 : IPlugin
{
#region Secure/Unsecure Configuration Setup
private string _secureConfig = null;
private string _unsecureConfig = null; public PluginClass2(string unsecureConfig, string secureConfig)
{
_secureConfig = secureConfig;
_unsecureConfig = unsecureConfig;
}
#endregion
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId); try
{
//TODO: Do stuff
#region 自定义代码
if (context.MessageName.Equals("Create"))
{
switch (context.Stage)
{
case ://创建 plugin step 的时候设置 create Pre-operation
CreatePreAction(tracer, context, service, factory);
break;
case ://创建 plugin step 的时候设置 create Post-operation
CreatePostAction(tracer, context, service, factory);
break;
default:
break;
}
}
else if (context.MessageName.Equals("Update"))
{
switch (context.Stage)
{
case ://创建 plugin step 的时候设置 update Pre-operation
UpdatePreAction(tracer, context, service, factory);
break;
case ://创建 plugin step 的时候设置 update Post-operation
UpdatePostAction(tracer, context, service, factory);
break;
default:
break;
}
}
#endregion
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(e.Message);
}
} #region 自定义函数
private void CreatePreAction(ITracingService tracer, IPluginExecutionContext context, IOrganizationService service, IOrganizationServiceFactory factory)
{
//创建当前 entity 时,在存到数据库之前调用
Entity entity = (Entity)context.InputParameters["Target"];
} private void CreatePostAction(ITracingService tracer, IPluginExecutionContext context, IOrganizationService service, IOrganizationServiceFactory factory)
{
//创建当前 entity 时,在存到数据库之后调用
} private void UpdatePreAction(ITracingService tracer, IPluginExecutionContext context, IOrganizationService service, IOrganizationServiceFactory factory)
{
//更新当前 entity 时,在存到数据库之前调用
} private void UpdatePostAction(ITracingService tracer, IPluginExecutionContext context, IOrganizationService service, IOrganizationServiceFactory factory)
{
//更新当前 entity 时,在存到数据库之后调用
}
#endregion
}
}
Workflow Class:
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities; namespace CRMPluginProject_Test
{
public class WorkflowClass1 : CodeActivity
{
protected override void Execute(CodeActivityContext executionContext)
{
ITracingService tracer = executionContext.GetExtension<ITracingService>();
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); try
{
Entity entity = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new Microsoft.Xrm.Sdk.Query.ColumnSet(true)); //TODO: Do stuff #region 自定义代码
//通过 workflow 创建一个 task
tracer.Trace("Start to create Task");
if (string.IsNullOrEmpty(entity["new_name"].ToString()))
{
Entities.Task task = new Entities.Task();
task.Subject = "task: " + entity["new_name"].ToString();
task.Description = "try to ceate a task";
task.RegardingObjectId = new EntityReference(entity.LogicalName, entity.Id);
service.Create(task);
}
#endregion
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(e.Message);
}
}
}
}
Dynamics CRM - Plug-in Class 和 Workflow Class 的用法与区别的更多相关文章
- 创建一个dynamics CRM workflow (一) - Introduction to Custom Workflows
Workflow: Use this process to model and automate real world business processes. These processes can ...
- Step by step Dynamics CRM 2011升级到Dynamics CRM 2013
原创地址:http://www.cnblogs.com/jfzhu/p/4018153.html 转载请注明出处 (一)检查Customizations 从2011升级到2013有一些legacy f ...
- 一、Microsoft Dynamics CRM 4.0 SDK概述
Chapter 1. Microsoft Dynamics CRM 4.0 SDK Overview(SDK概述) You are probably reading this book because ...
- Dynamics CRM 2013 初体验(3):新增加的功能
新系统除了修补系统历史漏洞外当然还会添加些比较有意思的新功能,至于这些新功能是否好用那就得看它是否能经过咱们这些使用者的考验了.Dynamics CRM 2013系统将不再支持Dynamics CRM ...
- How to set up Dynamics CRM 2011 development environment
Recently I have been starting to learn Microsoft Dynamics CRM 2011 about implement plugin and workfl ...
- Dynamics CRM中一个查找字段引发的【血案】
摘要: 本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复267或者20180311可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyon ...
- Step by Step 开发dynamics CRM
这里是作为开发贴的总结. 现在plugin和workflow系列已经终结. 希望这些教程能给想入坑的小伙伴一些帮忙. CRM中文教材不多, 我会不断努力为大家提供更优质的教程. Plugin 开发系列 ...
- Dynamics CRM 2016 Web API 消息列表
Function Name Description CalculateTotalTimeIncident Function Calculates the total time, in minutes, ...
- 定制Dynamics CRM标准导出功能:不能导出指定列的值
关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复239或者20161203可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...
随机推荐
- 2019-9-16 java上课知识整理总结(动手动脑,课后实验)
java上课知识整理总结(动手动脑,课后实验) 一,课堂测试 1,题目:课堂测试:像二柱子那样,花二十分钟写一个能自动生成30道小学四则运算题目的 “软件” 要求:(1)题目避免重复: (2)可定制( ...
- 20 - CommonJS - 规范的具体内容
- oracle(3)select语句中常用的关键字说明
1.select 查询表中的数据 select * from stu: ---查询stu表所有的数据,*代表所有2.dual ,伪表,要查询的数据不存在任何表中时使用 select sysdate f ...
- oracle 查询char类型的数据
曾经遇到一个坑. ';//使用PLSQL工具 能查出结果 偏偏在java代码里面查询不出结果. select taskdate from taskinfo where taskdate='201808 ...
- LabVIEW面向对象的ActorFramework(3)
四.LabVIEW面向对象的编程架构:Actor Framework Actor Framework是一个软件类库,用以支持编写有多个VI独立运行且相互间可通信的应用程序,在该类型应用程序中,每个VI ...
- CSS font-family 各字体一览表
windows常见内置中文字体字体中文名 字体英文名宋体 SimSun(浏览器默认) 黑体 SimHei 微软雅黑 ...
- 使用Spring Data Mongodb的MongoRepository类进行增删改查
Spring Data Mongodb提供一套快捷操作 mongodb的方法,创建Dao,继承MongoRepository,并指定实体类型和主键类型. public interface CmsPag ...
- C++ CreateInstance("ADODB.Connection");创建接口失败的解决方法
数据库对象mssql2005sp3专业版: 一般数据引用该路径文件#import "c:\\program files\\common files\\system\\ado\\msado15 ...
- kubelet组件详解---链接
https://blog.csdn.net/jettery/article/details/78891733
- Mdb文件工具类,UCanAccess使用,Access数据库操作
================================ ©Copyright 蕃薯耀 2020-01-09 https://www.cnblogs.com/fanshuyao/ 使用Ocbc ...