Mscrm 二次开发之 plugin开发
----------作者:Seer Lin
1,主要流程:
1),在crm里面,当我们对表单进行提交时,事件交由执行管道(Pipeline)去执行,事件执行管道在执行的过程中,又可以引发其他管道的执行,被引发执行的这个管道我们称我"子管道".
2),每一个管道的执行过程大体上可以分为三部分:PreStage,ExecutingStage,PostStage;PreStage是我们点保存按钮后系统保存事件执行之前,ExecutingStage是系统的执行事件(将数据保存到数据库),PostStage是系统事件执行完毕后.
2,开发步骤:
Plugin开发所要做的事情就是在管道的PreStage或者PostStage做我们想要的功能,例如你如果希望在创建一个Account(客户)的时候自动产生一个值作为客户编号accountnumber,那么你可以写一个plugin,然后在Execute 方法里面给实体的accountnumber属性赋值,再将这个plug-in用crm自带的注册工具注册到管道的PreStage;如果你想在创建完一个Account(客户)后顺便创建一个task(任务)去跟进这个客户,那么你可以写一个plug-in,然后将这个plug-in注册到管道的PostStage;详细步骤如下:
1), 创建一个Class library的项目,并将C:\Program Files\Microsoft Dynamics CRM\Server\bin\assembly这个文件夹里面microsoft.crm.sdk.dll和microsoft.crm.sdktypeproxy.dll这两个文件添加到引用.
2),实现IPlugin接口,并实现里面的Execute(IPluginExecutionContext context)方法,系统源码我没有仔细研究,但我估计是用aop的设计模式,系统执行管道的时候便会调用插件中这个Execute的方法;系统管道执行过程可以用同步的方式也可以用异步的方式,在注册工具可以选择.
3),将程序进行强签名,密码随便写
4),注册plug-in和step
l Message是指消息类型.因为我们是想在创建Acocunt的时候触发事件,因此我们将Message填为:create
l Primary Entity是我们要创建的实体类型
l Asynchronous是指异步方式执行.假如你执行一个事件要等很长时间,那你就应该选这个,这样用户才不会在界面等很久,因为你如果选同步的方式执行,事件的执行过程,用户UI是卡住的.
l Synchronous是指同步的方式执行,对应那些必须等待所以的事件执行完后才可以做其他操作的,就必须指定为同步
l Triggering是指注册在父管道还是子管道.举个例子,假如你将一个商机转为定单,这个时候系统会做两件事:1,先创建一个定单;2,将商机产品转为定单产品.其中1是在父管道执行,2是在子管道执行;如果你的plugin是监控商机,那就要注册到Parent Pipeline,如果你要监控产品,就要注册到Child Pipeline
l Step Department是指部署方式.Server是插件可以在服务器上运行,Offline是插件可以在离线状态下执行,但很多实体不支持,所以一般不选.
5),插件调试
如果你的插件出现了问题,你还可以进行调试,步骤如下
1)你的plug-in必须复制到C:\Program Files\Microsoft Dynamics CRM\Server\bin\assembl这个文件下面
2),你的plug-in必须注册为disk模式
l database会将plug-in注册到数据库,对应分布式应用来说,一般选这个,否则你必须将plug-in复制到每台服务器
l disk是将plug-in部署到硬盘,调试的时候必备
l GAC是部署到操作系统的GAC文件下面,比较少用
3),在程序代码加断点,然后选择”调试à附加到进程”,附加到w3wp.exe这个进程
===============================================
因为图片比较麻烦插入,所以我整理成一个word文件,图文并茂,应该比较容易理解,希望对初学者有帮助
/Files/seerlin/mscrm_plugin开发.doc
=======================================================================================
关键代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
namespace PluginTest
{
/// <summary>
/// Description:Demo of plug-in development
/// Author:Seer Lin
/// </summary>
public class PreCreateAccount : IPlugin
{
/// <summary>
/// Auto generate a random number as accountnumber.
/// </summary>
/// <param name="context"></param>
public void Execute(IPluginExecutionContext context)
{
if (context.InputParameters.Properties.Contains("Target") && context.InputParameters.Properties["Target"] is DynamicEntity)
{
DynamicEntity entity = context.InputParameters.Properties["Target"] as DynamicEntity;
if (entity.Properties.Contains("accountnumber"))
{
throw new InvalidPluginExecutionException("accountnumber can only be set by system");
}
else
{
StringProperty property = new StringProperty();
property.Name = "accountnumber";
property.Value = new Random().Next().ToString();//Generate a random number as accountnumber.
entity.Properties.Add(property);
}
}
}
}
public class PostCreateAccount : IPlugin
{
/// <summary>
/// Create a task activity to follow up with the account customer in 7 days when the account is created.
/// </summary>
public void Execute(IPluginExecutionContext context)
{
try
{
task followup = new task();
followup.subject = "send a email to customer";
followup.description = "follow up with the customer";
followup.category = context.PrimaryEntityName;
followup.scheduledstart
= followup.scheduledend
= CrmTypes.CreateCrmDateTimeFromUser(DateTime.Now.AddDays(7));
//It will return an id after you create a account.
if (context.OutputParameters.Properties.Contains("id"))
{
Guid accountid = new Guid(context.OutputParameters.Properties["id"].ToString());
followup.regardingobjectid = new Lookup(EntityName.account.ToString(), accountid);
}
//Value true if you want to use current user to create the task.
//Value false if you want to use administrator to create the task.
ICrmService service = context.CreateCrmService(true);
service.Create(followup);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
throw new InvalidPluginExecutionException("An error occourred in CreateAccountHandler plug-in.", ex);
}
}
}
}