CRM4.0中Plug-In使用

 

1) CRM4.0Plug-In说明

Plug-In是一个外部的Assembly被置入到CRM内部用来截取事件并可以和CRM进行交互,处理一些复杂逻辑。常见的可以截取的事件有一个实体的created, updated, or deleted等。Plug-In支持多重租赁,同步和异步运行,离线支持以及统一事件框架等。Plug-in通常会被应用于以下场景:

执行一个复杂的业务逻辑-在无法使用或很难使用Javascript和Workflow来处理时

在创建/更新实体时从其它系统获取数据

从CRM中更新其它系统

2)       CRM4.0Plug-In类创建

创建CRM4.0的Plug-In通常需要创建一个类库(Class Library)类型的项目。Microsoft Dynamics CRM 4.0是基于.NET Framework 2.0的,所以我们需要创建基于.NET Framework 2.0的项目,但是你可以用Visual Basic或者C#.

以下步骤演示了如何创建一个简单的CRM 4.0 plug-in 项目。

1)         创建C# Class Library类型项目,命名为myPlugIn

2)         添加对”Microsoft.Crm.Sdk.dll”和”Microsoft.Crm.SdkTypeProxy.dll”的引用。这两个DLLSDK"Bin目录中。

3)         在类文件中添加对Microsoft.Crm.SdkMicrosoft.Crm.SdkTypeProxy命名空间的应用,并且将类继承于IPlugin接口。

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.Crm.Sdk;

using Microsoft.Crm.SdkTypeProxy;

namespace SamplePlugins

{

public class Class1 : IPlugin

{

}

}

 

4)         实现IPlugin接口的方法-如果你的类继承了IPlugin接口,那么Execute方法是必须被实现的。

5)         作为演示我们让这个Plugin在创建一个Account时(后)将它的Unique Identifier(AccountID)更新到Account Description字段里。

      public void Execute(IPluginExecutionContext context)

        {

         DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties["Target"];

            if (entity.Name == EntityName.account.ToString())

            {

                Guid id = (Guid)context.OutputParameters.Properties["Id"];

                Key myKey = new Key(id);

                if (!entity.Properties.Contains("accountid"))

                {   entity.Properties["accountid"] = myKey;

                }

                if (!entity.Properties.Contains("description"))

                {

                    entity.Properties["description"] = "GUID = " + id.ToString();

                    ICrmService service = context.CreateCrmService(true);

                    service.Update(entity);

                }

            }

        }

 

6)          程序功能的实现就算完成了。但作为可以部署的DLL(为了安全的缘故)我们必须对这个项目实行强命名。

7)          对项目进行强命名,在Solution Explorer中右键点击项目名称然后选择Properties -> Signing. 选择”Sign the assembly”复选框并通过”New…”下拉选项来添加新的SNK文件。保存各种更改,Build项目,下一步就是部署了。

具体请参见附件:

CRM4.0中Plug-In使用

 

posted @ 2009-10-23 13:41  idealwhm  阅读(495)  评论(0编辑  收藏  举报