Microsoft Dynamics CRM 2011 Plugin 插件开发和调试

一、搭建环境:

     安装一个虚拟机,配置CRM2011 环境,具体:Microsoft CRM2011 安装步骤。

     本机环境:Microsoft Windows 7,Microsoft Sql Server 2008,Microsoft Visual Studio 2010。

     下载MS CRM 2001 SDK  http://www.microsoft.com/zh-cn/download/details.aspx?id=24004

      找到 sdk\tools\developertoolkit 下面的 crmdevelopertools_installer.msi 进行安装。

       问题:

1.创建实体商务审批、商务审批明细,其中 商务审批和商务审批明细是一对多的关系
2.商务审批字段
  编号、汇总明细价格、商务审批状态(草稿,审批中、审批通过、驳回)
3.商务审批明细字段
  编号、价格、商务审批状态(草稿,审批中、审批通过、驳回)

实现开发
1).将商务审批明细中的价格汇总到商务审批汇总明细价格中。

     1. 打开VS2010,点击文件,选择新建项目,选择Dynamics CRM模版,选择Dynamics CRM 2011 Plug-in Library,我将名称命名为PluginsTest,点击确定,如图:

点击确定之后,VS会自动生成一些代码。不管它。

2. 打开Plugin.cs,创建默认的构造函数: public Plugin() { } //创建默认的构造函数 必须的。 ---如果不创建默认的构造函数,那么会注册插件失败。

比如:当我新建一条商务审批记录的时候,会出现:

所以 注册插件的时候,必须创建默认的构造函数。

3.在Plugin.cs 找到这个代码:

// Use the factory to generate the Organization Service.

this.OrganizationService = factory.CreateOrganizationService(this.PluginExecutionContext.UserId);//当前用户权限的组织服务
this.OrganizationService = factory.CreateOrganizationService(null);//SYSTEM(系统内置最高权限)的组织服务

然后在下面写代码。

主要代码如下:

    #region 将商务审批明细中的价格汇总到商务审批汇总明细价格中
    //判断执行上下问验证是否存在Target参数并且为Entity
    if (PluginExecutionContext.InputParameters.Contains("Target") && PluginExecutionContext.InputParameters["Target"] is Entity)
    {
    try
    {
        if (PluginExecutionContext.MessageName != "Create" &&
        PluginExecutionContext.MessageName != "Update" &&
        PluginExecutionContext.MessageName != "Delete") { return; } //若不为Create,Update,Delete,则返回

        //获取目标实体对象
        Entity targetEntity = PluginExecutionContext.InputParameters["Target"] as Entity;

        //定义并初始化商务审批ID
        Guid swspid = Guid.Empty;

        #region Create
        if (PluginExecutionContext.MessageName == "Create")//若为Create 创建商务审批明细
        {
            if (targetEntity.Contains("new_swsp"))//判断是否存在new_swspid
            {
                swspid = (targetEntity["new_swsp"] as EntityReference).Id;//获取商务审批ID
            }
        }
        #endregion

    #region Update
    if (PluginExecutionContext.MessageName == "Update")//若为Update
    {
        Entity imageEntity = PluginExecutionContext.PreEntityImages["preImage"] as Entity;//获取执行上下文的前期映像操作

    if (targetEntity.Contains("new_jg"))
    {
        if (targetEntity != null && targetEntity.Contains("new_swsp"))
        {
            swspid = (targetEntity["new_swsp"] as EntityReference).Id;//获取商务审批ID
        }
        else
        {
             if (imageEntity.Contains("new_swsp")) { swspid = (imageEntity["new_swsp"] as EntityReference).Id; }
        }
     }
    }
    #endregion

    #region Delete
    if (PluginExecutionContext.MessageName == "Delete")//若为Delete
    {
        Entity imageEntity = PluginExecutionContext.PreEntityImages["preImage"] as Entity;
        //Entity imageEntity = PluginExecutionContext.PostEntityImages["postImage"] as Entity;

    if (targetEntity != null && targetEntity.Contains("new_swsp"))
    {
        swspid = (targetEntity["new_swsp"] as EntityReference).Id;
     }
    else
    {
        if (imageEntity.Contains("new_swsp"))
        {
            swspid = (imageEntity["new_swsp"] as EntityReference).Id;//获取商务审批ID的值
        }
    }
    }
    #endregion

    if (swspid != Guid.Empty)//如swspid不为空
    {
        GetTotalPriceById(swspid);//执行方法 将商务审批明细中的价格汇总到商务审批汇总明细价格中
    }
    }
    catch (Exception ex)
    {
        TracingService.Trace("Operate Error:{0}", ex.Message);
        throw ex;
    }
    }
    #endregion

 

    #region 通过商务审批明细的id 汇总价格 
    /// <summary>
    /// 通过商务审批明细的id 汇总价格
    /// </summary>
    /// <param name="spid"></param>
    private void GetTotalPriceById(Guid spid)
    {
    //通过高级查找聚合查询 下载fetchxml,进行修改。
    string fetchxml = @"<fetch version='1.0' mapping='logical' distinct='false' aggregate='true'>
    <entity name='new_swspmx'>
    <attribute name='new_jg' alias='new_jg_sum' aggregate='sum' />
    <filter type='and'>
    <condition attribute='statecode' operator='eq' value='0' />
    <condition attribute='new_swsp' operator='eq' value='" + spid + @"' />
    </filter>
    </entity>
    </fetch>";

    EntityCollection entityCollection = OrganizationService.RetrieveMultiple(new FetchExpression(fetchxml));//获取集合信息

    if (entityCollection.Entities.Count == 0) { return; }
    decimal new_jg = 0;

    foreach(var item in entityCollection.Entities)
    {
        if (item.Contains("new_jg_sum"))
        {
            new_jg = ((Money)(((AliasedValue)item["new_jg_sum"]).Value)).Value;
            Entity swspEntity = new Entity("new_swsp");//实例化商务审批实体
            swspEntity.Attributes.Add("new_swspid",spid);//添加商务审批id
            swspEntity.Attributes.Add("new_hzmxjg", new Money(new_jg));//添加商务审批价格
            OrganizationService.Update(swspEntity);//执行Update操作
        }
    }

}
#endregion

然后重新生成

右击类库PluginsTest 属性,为Plugin.cs签名。

签名之后,点击保存文件。

然后重新生成PluginsTest类库。

二、调试插件

下载 PluginRegistration.exe 

打开  PluginRegistration.exe 

打开如图所示:

我们先来调试 当创建一条商务审批明细记录出现报错信息的时候。

第一:首先创建一个新的connection. 选择你虚拟机的ip地址,用户名,如图:

然后点击Connect,出现

出入正确的密码。点击确定之后会出现 

再点击Connect 就OK了。

第二:注册插件

1. 在Plugin Registration Tool 窗体上 Register :

选择 Register New Assembly (注册新的程序集) 。选择PluginTest生成的dll路径:

点击Register Selected之后:

再选择 

出现: 

选择 Register New Step,会弹出一个新的窗体:

说明下:Message:当输入C的时候,界面会自动提示。---执行什么操作   Primary Entity:操作的主实体。这里为new_swspmx(商务审批明细)。

选择Post-operation。点Register New就可以了。

然后出现:

选择(Step)PluginTest.Plugin:Create of new_swspmx。选择上面的 Profile(注意要先点击install Profiler,才会有Profile).

出现:

然后进行CRM页面上创建一条商务审批明细记录。

首先,创建一条商务审批记录:

点击保存之后,选择商务审批窗体 左边导航下面的 商务审批明细:

然后创建一条新的商务审批明细记录,输入基本的信息,点击保存,

会弹出一个日志报错信息(注意要出现业务流程错误这样子很长的信息,才可以调试),然后下载日志报错信息,我这里把日志报错信息放到了电脑桌面上:

接着,PluginRegistration.exe上面点击 Stop Profiling,会变成Profile

然后选择 Debug,选择报错的日志文件:ErrorDetails.txt ,选择dll的路径,出现:

然后在VS里面设置断点,选择调试,附件进程:

在点击前面的 Start plugin 就可以,调试了。按F10,F11

基本上就这样子,谢谢。

注意 :这里一个情况就是停用和启用的时候没有考虑到,代码就不写了,大家可以自己加上。

原文地址:https://www.cnblogs.com/allenhua/p/2818537.html