CRM 4.0 开发同 SharePoint Server 开发一样提供 Web Services 和 DLL 两种方式的开发接口。
对于 Web Services 接口,又有两种不同的引用方式:一种是在项目中直接添加 Web 引用,把 CRM 4.0 的 Web Services(如http://localhost:5555/mscrmservices/2007/CrmService.asmx)直接 Web 引用到项目中生成代理类;另一种方式,是先在 CRM 4.0 的 Settings –> Customization 中选择 Download Web Service Description Files,下载其中的WSDL文件,然后再在项目 Web 引用中直接直接保存在本地的WSDL文件亦可。这两种方式归根到底都是在客户端建立服务器端 Web Services 的代理类,只是做法上不同而已。对于使用 Web Services 接口开发的项目,最佳做法是在代码中通过配置或硬编码再次指定 Web Services 的 URL 地址。
对于 DLL 方式,在安装好 CRM 4.0 SDK 后,就直接在项目中引用 Microsoft CRM 4.0 SDK\bin 下的 microsoft.crm.sdk.dll、microsoft.crm.sdktypeproxy.dll 和 microsoft.crm.outlook.sdk.dll 三个文件即可进行开发。
下面第一个 CRM 4.0 程序就是直接用 Web Services 方式的一个范例。
1、新建一个Console Application,按默认命名为ConsoleApplication1。
2、在该项目中添加Web引用,Web服务地址类似为:http://localhost:5555/mscrmservices/2007/CrmService.asmx。其中将 localhost:5555 替换成实际 CRM 服务所在的服务器地址和端口号。把该Web引用命名为 CrmSdk。
3、编辑 Program.cs 文件,其代码如下:
using System; using System.Collections.Generic; using System.Text;
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // 名称是 A. Datum Corporation 的 Account 的 ID Guid accountId = new Guid("D04D44AD-36D0-DC11-AA32-0003FF33509E"); // 根据 ID 输出该 Account 的信息 RetrieveAccount(accountId);
Console.ReadLine(); } public static void RetrieveAccount(Guid accountId) { // 根据 Organization Name 和 Server 创建 CrmService 实例。 // 此处 OrgName 是 MicrosoftCRM,服务器 Server 名称是 MOSS。 CrmSdk.CrmService service = GetCrmService("MicrosoftCRM", "MOSS"); // 要显示的 Account 的 Attribute 字段。 CrmSdk.ColumnSet cols = new CrmSdk.ColumnSet(); cols.Attributes = new string[] { "name", "telephone1", "address1_city", "address1_country" }; try { // 获取该 Account 的实例 CrmSdk.account oAccount = (CrmSdk.account)service.Retrieve(CrmSdk.EntityName.account.ToString(), accountId, cols); // 输出显示 Account 的一些属性 Console.WriteLine("Account Name: {0}", oAccount.name); Console.WriteLine("Main Phone: {0}", oAccount.telephone1); Console.WriteLine("City - Country: {0} - {1}", oAccount.address1_city, oAccount.address1_country); } catch(System.Web.Services.Protocols.SoapException ex) { Console.WriteLine(ex.Detail.InnerText); } }
public static CrmSdk.CrmService GetCrmService(string orgName, string server) { // 创建 CrmAuthenticationToken 实例 CrmSdk.CrmAuthenticationToken token = new CrmSdk.CrmAuthenticationToken(); token.AuthenticationType = 0; //AD (On-premise) token.OrganizationName = orgName;
CrmSdk.CrmService service = new CrmSdk.CrmService(); //service.Credentials = System.Net.CredentialCache.DefaultCredentials;
// If you know you are using the default credentials, // you can replace the service.Crendentials line with the following line service.UseDefaultCredentials = true; service.CrmAuthenticationTokenValue = token; service.Url = string.Format("http://{0}:5555/mscrmservices/2007/crmservice.asmx", server);
return service; } } }
4、运行即可输出该 Account 的一些基本信息。
根据上述编程,可以稍微发散思考下:CRM 中的 Entity 是不是很类似数据库的表,有字段,有关系。只是 CRM 通过 Web 界面直接定义结构和关系,而数据库则通过专门数据库管理工具。上述编程中从创建 CrmService 实例到获取 Entity 实例,再到最后显示其信息,是一种固定编程模式,是不是类似数据库创建连接到相应表操作。再发散下,都知道数据库表模型很重要,因此在 CRM 开发时,是不是对 Entity 的定义及其字段属性的制定都需要详细考虑。