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. ...
随机推荐
- webpack散记---提取公共代码
(1)作用: 减少代码冗余 提高加载速度 (2)来源 commonsChunkPlugin webpack.optimize.CommonsChunkPlugin (3)配置 { plugins:[ ...
- (java) webdriver 启动firefox driver时,加载firebug的扩展
去网上下载一个firebug.xpi(对应版本, 我的ff是17,可以使用firebug-1.11.4.xpi,最好使用非firefox浏览器下载,不然提示你直接安装到firefox) @Before ...
- Java Web学生信息保存
Course.javapackage entity; public class Course { private int id; private String num; private String ...
- Java入门程序开发
Java入门程序开发 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Java程序开发流程 Java程序开发流程如下: >.将Java代码编写到扩展名为".jav ...
- windows driver 获取文件属性
OBJECT_ATTRIBUTES oa; FILE_NETWORK_OPEN_INFORMATION fnoi; UNICODE_STRING strPath = RTL_CONSTANT_STRI ...
- Egret - EUI - 隐藏滚动条
<e:Skin> <e:VScrollBar autoVisibility="false" visible="false"/> < ...
- 洛谷 P2543 [AHOI2004]奇怪的字符串
题目传送门 解题思路: 本题朴素求最长公共子序列即可,但是空间不够,怎么办呢? 空间不够,滚动数组来救 AC代码: #include<iostream> #include<cstdi ...
- Discuz中常用的编辑器代码
.[ b]文字:在文字的位置可以任意加入您需要的字符,显示为粗体效果. .[ i]文字:在文字的位置可以任意加入您需要的字符,显示为斜体效果. .[ u]文字:在文字的位置可以任意加入您需要的字符,显 ...
- android——TextView默认文字开发时显示运行时隐藏
根布局添加属性: xmlns:tools="http://schemas.android.com/tools" textview添加属性: tools:text="默认文 ...
- java课程之团队开发冲刺阶段2.6
总结昨天进度: 1.总体的思路已经完成,代码也差不多了,只剩下对闹钟activity的设置 遇到的困难: 1.在设置震动的时候,对方法有点不太理解,所以使用的时候产生了错误,没有达到预期的效果 今天的 ...