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. ...
随机推荐
- js 加密解密 TripleDES
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- leetcode 正则表达式 (动态规划)
给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 '.' 和 '*' 的正则表达式匹配. '.' 匹配任意单个字符'*' 匹配零个或多个前面的那一个元素所谓匹配,是要涵盖 整个 字符串 s的 ...
- 【数据库】Function&Procedure&Package
Function/Procedure都是可独立编译并存储在数据库中的,区别是Function有返回值. Package则是数据和过程.函数的集合体. CREATE PROCEDURE dorepeat ...
- Java的优先队列PriorityQueue详解
一.优先队列概述 优先队列PriorityQueue是Queue接口的实现,可以对其中元素进行排序, 可以放基本数据类型的包装类(如:Integer,Long等)或自定义的类 对于基本数据类型的包装器 ...
- IO_课堂测试
IO_课堂测试 一,用户需求 英语的26 个字母的频率在一本小说中是如何分布的?某类型文章中常出现的单词是什么?某作家最常用的词汇是什么?<飘> 中最常用的短语是什么,等等. (1)要求1 ...
- 51nod 1191:消灭兔子 贪心+优先队列
1191 消灭兔子 题目来源: 2013腾讯马拉松赛第三场 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 有N只兔子,每只有一个血量B[i],需要 ...
- datetime使用
通过当前日期,获取最近第一个周五.第二个周五.每季度末最后一个周五 def get_current_week(self, symbol: str, start_date: datetime): i ...
- php mysql 由于目标计算机积极拒绝,无法连接 错误原因
除了在网上百度的那些外,我的机器发生了这个错误 我需要用php远程连接mysql,在目标机上已经给了客户机权限,可是还是发生“ 由于目标计算机积极拒绝,无法连接”错误 在客户机上直接用终端连接目标机的 ...
- BZOJ:2244: [SDOI2011]拦截导弹
问题: printf("%.5f ",0):为什么错了? 注意: 初始值很重要 题解: 三维偏序问题: 记录从前往后最长上升子序列长度pref,条数preg 从后往前suff,su ...
- 在swift调用OC的第三方库
https://www.jianshu.com/p/4799ac1d7dce 2017.06.02 23:55* 字数 275 阅读 1619评论 0喜欢 3 环境:xcode 8.3.2 系统: M ...