Microsoft Dynamics CRM - Web服务

Microsoft Dynamics CRM提供两种重要的Web服务,用于从外部应用程序访问CRM并调用Web方法以执行常见的业务数据操作,如在CRM中创建,删除,更新和查找.

考虑以下场景 :

  • 您有一个外部.NET应用程序,需要与之交谈CRM.例如,您可能希望在外部应用程序中注册新客户时在CRM中插入联系人记录.

  • 或者,您可能想要搜索记录在CRM中并在外部应用程序中显示搜索结果.

在这种情况下,您可以使用CRM公开的Web服务来消费它们在您的应用程序中,并在CRM中执行创建,删除,更新和查找操作.

IDiscoveryService Web服务

此Web服务返回一个列表指定用户所属的组织以及每个组织的URL端点.

IOrganizationService Web服务

此Web服务是主要Web服务用于访问CRM中的数据和元数据. IOrganizationService使用两个重要的程序集 -   Microsoft.Xrm.Sdk.dll Microsoft.Crm.Sdk.Proxy.dll .这些程序集可以在 Bin 文件夹中的CRM SDK包中找到.

Microsoft.Xrm.Sdk.dll

此程序集定义核心xRM方法和类型,包括使Microsoft Dynamics CRM连接更简单的代理类,身份验证方法和服务合同.

Microsoft.Crm.Sdk.Proxy.dll

此程序集定义非核心消息的请求和响应以及使用组织数据所需的枚举.以下是这两个程序集支持的名称空间.

这些程序集中的每一个都支持某些消息,这些消息将用于处理存储在任何实体中的数据.可以在以下链接中找到它们支持的完整消息列表 :

支持的xRM消息 :   https://msdn.microsoft.com/en-us/library/gg334698 .aspx

支持的CRM消息 :   https://msdn.microsoft.com/en-us/library/gg309482 .aspx

IOrganizationService Web服务方法

IOrganizationService 提供了八种允许您执行所有操作的方法系统和自定义实体以及组织元数据的常见操作.

Sr.No方法&描述
1

IOrganizationService.Create

创建记录.

2

IOrganizationService.Update

更新现有记录.

3

IOrganizationService.检索

检索记录.

4

IOrganizationService. RetrieveMultiple

检索记录集合.

5

IOrganizationService.删除

删除记录.

6

IOrganizationService.员工

在记录之间创建一个链接.

7

IOrganizationService.Disassociate

删除记录之间的链接.

8

IOrganizationService.Execute

用于常见的记录处理以及专业处理,如案例解析,重复检测等.

Web服务示例

要了解Web服务在CRM中的工作方式,我们将查看CRM SDK提供的示例.在此示例中,我们将创建一个新的帐户记录,更新它,然后最终使用CRM IOrganizationService Web服务将其删除.

步骤1 : 打开已解压缩CRM SDK的文件夹.现在,通过浏览到以下位置打开QuickStartCS.sln解决方案:SDK\SampleCode\CS\QuickStart

Mscrm Web服务示例步骤1

步骤2 : 我们将通过简化连接项目探索快速入门.在此项目中打开 app.config .默认情况下,此文件中的 connectionStrings 部分将被注释.

Mscrm Web服务示例步骤2

从此,取消注释第一个连接字符串键并编辑以下三个详细信息 :

Url :指定CRM实例的URL.在我们的案例中,由于我们使用的是在线版CRM,您必须提及该URL.

用户名 : 您的CRM Online用户名.

密码 : 您的CRM Online密码.

Mscrm Web服务示例步骤2 2

第3步 : 打开此项目中的 SimplifiedConnection.cs 文件,并在其中运行Runmethod.

public void Run(StringconnectionString, boolpromptforDelete) {
   try {
      
      // Establish a connection to the organization web service using CrmConnection.
      Microsoft.Xrm.Client.CrmConnection connection =
         CrmConnection.Parse(connectionString);
      
      // Obtain an organization service proxy.
      // The using statement assures that the service proxy will be properly disposed.
      using(_orgService = new OrganizationService(connection)) {

         //Create any entity records this sample requires.
         CreateRequiredRecords();
         
         // Obtain information about the logged on user from the web service.
         Guid userid = ((WhoAmIResponse)_orgService.Execute(new WhoAmIRequest())).UserId;
         SystemUser systemUser = (SystemUser)_orgService.Retrieve("systemuser",userid,
            new ColumnSet(newstring[]{"firstname","lastname"}));
         
         Console.WriteLine("Logged on user is {0} {1}.",
            systemUser.FirstName,systemUser.LastName);

         // Retrieve the version of Microsoft Dynamics CRM.
         RetrieveVersionRequest versionRequest = new RetrieveVersionRequest();
         RetrieveVersionResponse versionResponse =
            (RetrieveVersionResponse)_orgService.Execute(versionRequest);
         Console.WriteLine("Microsoft Dynamics CRM version {0}.",
            versionResponse.Version);
         
         // Instantiate an account object. Note the use of option set
         enumerations defined in OptionSets.cs.
         
         // Refer to the Entity Metadata topic in the SDK documentation to
         determine which attributes must
         
         // be set for each entity.
         Account account = new Account{Name = "Fourth Coffee"};
         account.AccountCategoryCode = new OptionSetValue(
            (int)AccountAccountCateg oryCode.PreferredCustomer);
         account.CustomerTypeCode = new OptionSetValue(
            (int)AccountCustomerTypeCod e.Investor);
         
         // Create an account record named Fourth Coffee.
         _accountId = _orgService.Create(account);
         Console.Write("{0} {1} created, ",account.LogicalName,account.Name);
         
         // Retrieve the several attributes from the new account.
         ColumnSet cols = new ColumnSet(
            new String[]{"name","address1_postalcode","lastusedincampaign"});
         Account retrievedAccount =
            (Account)_orgService.Retrieve("account", _accountId, cols);
         Console.Write("retrieved, ");

         // Update the postal code attribute.
         retrievedAccount.Address1_PostalCode = "98052";

         // The address 2 postal code was set accidentally, so set it to null.
         retrievedAccount.Address2_PostalCode = null;

         // Shows use of a Money value.
         retrievedAccount.Revenue = new Money(5000000);

         // Shows use of a Boolean value.
         retrievedAccount.CreditOnHold = false;
         
         // Update the account record.
         _orgService.Update(retrievedAccount);
         Console.WriteLine("and updated.");
         
         // Delete any entity records this sample created.
         DeleteRequiredRecords(promptforDelete);
      } 
   } 
   // Catch any service fault exceptions that Microsoft Dynamics CRM throws.
   catch(FaultException<microsoft.xrm.sdk.organizationservicefault>) {

      // You can handle an exception here or pass it back to the calling method.
      throw;
   }
}

第4步 : 此方法基本上演示了使用CRM Web服务的所有CRUD操作.代码首先创建组织实例,然后创建帐户记录,更新创建的记录,最后删除它.让我们看看这段代码的重要组成部分.要在运行此代码时查看CRM中的移动更改,您可以逐步调试此代码(如下所述)并同时查看CRM中的更改.

步骤4.1 : 使用我们在步骤2 中修改的连接字符串建立与组织的连接.

Microsoft.Xrm.Client.CrmConnection connection = CrmConnection.Parse(connectionString);

步骤4.2 : 获得CRM组织Web服务的代理实例.

_orgService = new OrganizationService(connection)

步骤4.3 : 创建一个新的Account实体对象并设置其Name,AccountCategoryCode和CustomerTypeCode.

Account account = new Account{Name = "Fifth Coffee"}; 
account.AccountCategoryCode = new OptionSetValue(
   (int)AccountAccountCategoryCode.P referredCustomer); 
account.CustomerTypeCode = new OptionSetValue(
   (int)AccountCustomerTypeCode.Investor);

步骤4.4 : 使用组织服务的Create方法创建新记录.

_accountId = _orgService.Create(account);

如果您导航到CRM,您将看到新创建的帐户记录.

Mscrm Web服务示例步骤4_4

步骤4.5 : 创建帐户后,服务将使用检索Web服务方法从CRM中检索记录.

ColumnSet cols = new ColumnSet(new String[]{
   "name","address1_postalcode","lastusedincampaign"}); 
Account retrievedAccount = 
   (Account)_orgService.Retrieve("account", _accountId, cols);

步骤4.6 : 获得检索到的记录后,您可以设置记录的更新值.

retrievedAccount.Address1_PostalCode = "98052"; 
retrievedAccount.Address2_PostalCode = null; 
retrievedAccount.Revenue = new Money(5000000); 
retrievedAccount.CreditOnHold = false;

步骤4.7 : 设置记录的更新值后,使用更新Web服务方法将记录更新回CRM数据库.

_orgService.Update(retrievedAccount);

如果您在CRM中打开记录,您会在那里看到这些值更新.

Mscrm Web服务示例步骤4_7

步骤4.8 : 最后,使用删除Web服务方法删除记录.

_orgService.Delete(Account.EntityLogicalName, _accountId);

如果您现在刷新CRM中的相同记录,您将看到该记录已被删除,因此该记录不再可用.

Mscrm Web服务示例步骤4_8

结论

在本章中,我们讨论了CRM提供的两个重要的Web服务,以及如何从外部应用程序使用这些Web服务来执行各种CRUD操作的工作示例.