標籤:
由於MSCRM的異構性和封閉性,許多其它的平台無法直接調用MSCRM提供的API介面,一般的處理方式是用.net編寫webservice,通過中間這一層轉換來使其它系統調用我們自己編寫的webservice.下面示範如何開發可調用MSCRM2013 API的webservice。
主要步驟:
- 建立asp.net web項目
- 編寫代碼
- 驗證服務
- 產生項目,並將相關檔案拷貝到CRM的指定路徑
一.建立項目
右鍵點擊資源管理員項目,並添加一個web服務,此處名稱為:MSCRMWebServiceDemo
引用相關的DLL檔案
二.編寫代碼
using Microsoft.Xrm.Sdk;using Microsoft.Xrm.Sdk.Client;using Microsoft.Xrm.Sdk.Query;using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.ServiceModel.Description;using System.Web;using System.Web.Services;namespace MSCRMWebServiceDemo{ /// <summary> /// MyMSCRMWebService 的摘要說明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允許使用 ASP.NET AJAX 從指令碼中調用此 Web 服務,請取消注釋以下行。 // [System.Web.Script.Services.ScriptService] public class MyMSCRMWebService : System.Web.Services.WebService { static private IOrganizationService GetOrganisationService() { ClientCredentials credentials = new ClientCredentials(); credentials.Windows.ClientCredential = new NetworkCredential("crmadmin", "password01!", "test"); OrganizationServiceProxy proxy = new OrganizationServiceProxy(new Uri("http://192.168.10.17/test/XRMServices/2011/Organization.svc"), null, credentials, null); return proxy as IOrganizationService; } [WebMethod] public string HelloWorld() { IOrganizationService service = GetOrganisationService(); //用FETCHXML的方式擷取會員資料 string fetch2 = @" <fetch mapping='logical'> <entity name='account'> <attribute name='name' /> <attribute name='address1_city' /> <attribute name='primarycontactid' /> <attribute name='telephone1' /> <attribute name='accountid' /> <order attribute='name' descending='false' /> <link-entity name='contact' from='contactid' to='primarycontactid' visible='false' link-type='outer' alias='accountprimarycontactidcontactcontactid'> <attribute name='emailaddress1' /> </link-entity> </entity></fetch>"; EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetch2)); String name = ""; foreach (var c in result.Entities) { name += c.Attributes["name"]; } return name; } }}三.點擊VS的運行按鈕,測試服務
四.部署相關項目至CRM指定路徑
拷貝MSCRMWebServiceDemo.dll至CRM的以下路徑:
X:\Program Files\Microsoft Dynamics CRM\CRMWeb\bin
拷貝MyMSCRMWebService.asmx至CRM的以下路徑:
C:\Program Files\Microsoft Dynamics CRM\CRMWeb\ISV
最後驗證一下webservice,開啟如下地址,出現以下介面則部署成功
如何在Dynamic CRM 2013中建立WebService介面供其它系統調用