在 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 的用法与区别的更多相关文章

  1. 创建一个dynamics CRM workflow (一) - Introduction to Custom Workflows

    Workflow: Use this process to model and automate real world business processes. These processes can ...

  2. Step by step Dynamics CRM 2011升级到Dynamics CRM 2013

    原创地址:http://www.cnblogs.com/jfzhu/p/4018153.html 转载请注明出处 (一)检查Customizations 从2011升级到2013有一些legacy f ...

  3. 一、Microsoft Dynamics CRM 4.0 SDK概述

    Chapter 1. Microsoft Dynamics CRM 4.0 SDK Overview(SDK概述) You are probably reading this book because ...

  4. Dynamics CRM 2013 初体验(3):新增加的功能

    新系统除了修补系统历史漏洞外当然还会添加些比较有意思的新功能,至于这些新功能是否好用那就得看它是否能经过咱们这些使用者的考验了.Dynamics CRM 2013系统将不再支持Dynamics CRM ...

  5. 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 ...

  6. Dynamics CRM中一个查找字段引发的【血案】

    摘要: 本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复267或者20180311可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyon ...

  7. Step by Step 开发dynamics CRM

    这里是作为开发贴的总结. 现在plugin和workflow系列已经终结. 希望这些教程能给想入坑的小伙伴一些帮忙. CRM中文教材不多, 我会不断努力为大家提供更优质的教程. Plugin 开发系列 ...

  8. Dynamics CRM 2016 Web API 消息列表

    Function Name Description CalculateTotalTimeIncident Function Calculates the total time, in minutes, ...

  9. 定制Dynamics CRM标准导出功能:不能导出指定列的值

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复239或者20161203可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...

随机推荐

  1. poj 1330 Nearest Common Ancestors 求最近祖先节点

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37386   Accept ...

  2. python 同步异步,并发并行,同步锁

    并发:系统具有处理多个任务(动作)的能力 并行:系统具有同时处理多个任务(动作)的能力 同步:当进程执行到一个IO(等待外部数据)的时候,需要等待,等待即同步 异步:当进程执行到一个IO(等待外部数据 ...

  3. TX2-刷机完成后安装程序ubuntu_linux命令&TX2学习总结

    Linux教程|菜鸟教程:http://www.runoob.com/linux/linux-tutorial.html 认识linux:ping命令:ping命令是常用的网络命令ping网关:pin ...

  4. C语言-存储类&作用域&生命周期&链接属性

    1.概念解析(1)存储类 a.存储类就是存储类型,也就是描述C语言变量在何种地方存储. b.内存有多种管理办法:栈.堆.数据段.bss段..text段......一个变量的存储类属性就是描述这个变量存 ...

  5. 屏幕切换 onStart() onStop() onRestart() onDestroy()

    android:configChanges="orientation|keyboardHidden|screenSize"          //xml文件<activity ...

  6. java课程课后作业190530之找水王

    从题目中我们可以看出,水王有着相当严苛的条件才可以成为,那就是必须拥有一半的评论量才可以当上水王.当然这就是破题的关键,最简单的算法当然是用O(N平方)的复杂度的那种算法,但显然,我们需要的不是这种. ...

  7. POJ 1502:MPI Maelstrom Dijkstra模板题

    MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6499   Accepted: 4036 Des ...

  8. redis常用命令--基础

    redis默认有个数据库,下标从开始,不支持自定义数据库名字. 使用select  index 切换数据库  : select 7 (切换到第八个数据库) dbsize:求当前数据库中键值对的数量. ...

  9. JS添加、设置属性以及鼠标移入移出事件

    源代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  10. dxSkinController1 皮肤使用

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...