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. ...
随机推荐
- ssh-keygen 签名ca证书
介绍 ssh-keygen命令用于为"ssh" 生成,管理和转换认证秘钥,支持RSA和DSA两种认证秘钥 生成秘钥对 ssh-keygen -b 2048 -C milo -f 2 ...
- jar类库加载顺序
当我们启动一个tomcat的服务的时候,jar包和claess文件加载顺序: 1. $java_home/lib 目录下的java核心api 2. $java_home/lib/ext 目录下的jav ...
- ES6模块化深入 debug
引子: 2020.2.24.最近刚写完一个vue项目.项目用到ES6的模块化 想到之前写node项目用到过commonjs模块化 就想着把所有用到过的模块化技术 总结学习一下 在看阮一峰老师的 es6 ...
- ansible简单部署前端
pipeline{ agent any parameters { choice(name: 'server_name', choices: ['xx','xx'], description: 'ser ...
- 2.在约会网站上使用k近邻算法
在约会网站上使用k近邻算法 思路步骤: 1. 收集数据:提供文本文件.2. 准备数据:使用Python解析文本文件.3. 分析数据:使用Matplotlib画二维扩散图.4. 训练算法:此步骤不适用于 ...
- 开始linux课程预习工作
预习的过程,就是在老师讲课之前,自己学习的过程.重点,难点,疑点,不等老师先讲,自己先趟一遍,老师在讲的时候,相信会吸收的更好一些.
- 题解 P1654 【OSU!】
题面 一序列\(a\), 对于每一个\(i\)均有\(a_i\)有\(p_i\)的几率为1, 否则为\(0\) 求: \(a\)中极长全\(1\)子序列长度三次方之和的期望 前置知识 基本期望(期望的 ...
- kubele常用配置
KUBELET_OPTS="--logtostderr=true \--v=4 \--hostname-override=10.83.52.147 \--kubeconfig=/usr/lo ...
- class选择器,外部样式表,选择器优先级
class选择器: 先在相应标签中设置一个class属性,如class=“class名”.class名{ ……css样式}注:class名以英文字母开头,可以多个标签重复使用.优先级:标签名选择器 & ...
- 在Azure Storage 托管HTTP静态网站
本文演示了在Azure Storage托管HTTP静态网站. 注意:HTTP已经不建议使用. 创建Azure StorageV2 存储账户 账户类型选择“StorageV2(通用版V2)”: 本例中, ...