目标:
Dynamics CRM 365 Online 测试Plugin开发,功能是实现自动编号Account Entity的accountnumber
步骤:
1、 使用Visual Studio 2019创建一个类库输出项目,此处要注意.net framework的版本,我使用dynamics CRM online 是v9.0需要安装如下二个包,注意它的版本要与CRM兼容
2、 编写实现自动编号的业务逻辑,此处我测试的编号规则是年月日时分秒拼在一起的逻辑
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace Fei_Crm_Plugin
{
public class Fei_AccountPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
try
{
DateTime _dateTime = DateTime.UtcNow;
var welcomeText = $"F_{_dateTime.Year.ToString()}{_dateTime.Month.ToString()}" +
$"{_dateTime.Day.ToString()}{_dateTime.Hour.ToString()}{_dateTime.Minute.ToString()}{_dateTime.Second.ToString()}";
entity.Attributes.Add("accountnumber", welcomeText);
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in Plug-in", ex);
}
catch(Exception ex)
{
tracingService.Trace($"Fei_Plugin:{ex.ToString()}");
throw;
}
}
}
}
}
3、打开Plugin registration Tool注册开发好的dll, 此处需注意,如果连不上CRM则需下载最新的Plugin Registration Tool, 可通过安装
Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool这个包下载所需的版本,
位置在vs项中,如我的在如下位置
E:\Fei\Visual_Studio_Projects\Fei_Dynamics_Crm_Plugin\packages\Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool.9.1.0.20\tools
输入用户名与密码连接CRM并加载
选择Register new Assembly,然后上传自已dll
4、 新建二条account记录,accountnumber字段自动编号,在CRM测试结果
产出:
Dynamics CRM 365 的plugin开发,可以实现通过power app配置无法实现的复杂逻辑