Dynamic CRM 创建WebService接口供其它系统调用

由于MSCRM的异构性和封闭性,许多其它的平台无法直接调用MSCRM提供的API接口,一般的处理方式是用.net编写webservice,通过中间这一层转换来使其它系统调用我们自己编写的webservice.下面演示如何开发可调用MSCRM2013 API的webservice。

主要步骤:

 

  1. 新建asp.net web项目
  2. 编写代码
  3. 验证服务
  4. 生成项目,并将相关文件拷贝到CRM的指定路径

 

 

一.新建项目

 

右键点击资源管理器项目,并添加一个web服务,此处名称为:MSCRMWebServiceDemo

引用相关的DLL文件

二.编写代码

 

  1.  
    using Microsoft.Xrm.Sdk;
  2.  
    using Microsoft.Xrm.Sdk.Client;
  3.  
    using Microsoft.Xrm.Sdk.Query;
  4.  
    using System;
  5.  
    using System.Collections.Generic;
  6.  
    using System.Linq;
  7.  
    using System.Net;
  8.  
    using System.ServiceModel.Description;
  9.  
    using System.Web;
  10.  
    using System.Web.Services;
  11.  
     
  12.  
     
  13.  
    namespace MSCRMWebServiceDemo
  14.  
    {
  15.  
    /// <summary>
  16.  
    /// MyMSCRMWebService 的摘要说明
  17.  
    /// </summary>
  18.  
    [ WebService(Namespace = "http://tempuri.org/")]
  19.  
    [ WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  20.  
    [ System.ComponentModel.ToolboxItem(false)]
  21.  
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
  22.  
    // [System.Web.Script.Services.ScriptService]
  23.  
    public class MyMSCRMWebService : System. Web. Services. WebService
  24.  
    {
  25.  
     
  26.  
    static private IOrganizationService GetOrganisationService()
  27.  
    {
  28.  
    ClientCredentials credentials = new ClientCredentials();
  29.  
    credentials.Windows.ClientCredential = new NetworkCredential( "crmadmin", "password01!", "test");
  30.  
     
  31.  
    OrganizationServiceProxy proxy = new OrganizationServiceProxy( new Uri( "http://192.168.10.17/test/XRMServices/2011/Organization.svc"), null, credentials, null);
  32.  
     
  33.  
    return proxy as IOrganizationService;
  34.  
    }
  35.  
     
  36.  
    [ WebMethod]
  37.  
    public string HelloWorld()
  38.  
    {
  39.  
     
  40.  
    IOrganizationService service = GetOrganisationService();
  41.  
    //用FETCHXML的方式获取会员数据
  42.  
    string fetch2 = @"
  43.  
    <fetch mapping='logical'>
  44.  
    <entity name='account'>
  45.  
    <attribute name='name' />
  46.  
    <attribute name='address1_city' />
  47.  
    <attribute name='primarycontactid' />
  48.  
    <attribute name='telephone1' />
  49.  
    <attribute name='accountid' />
  50.  
    <order attribute='name' descending='false' />
  51.  
    <link-entity name='contact' from='contactid' to='primarycontactid' visible='false' link-type='outer' alias='accountprimarycontactidcontactcontactid'>
  52.  
    <attribute name='emailaddress1' />
  53.  
    </link-entity>
  54.  
    </entity>
  55.  
    </fetch>";
  56.  
     
  57.  
    EntityCollection result = service.RetrieveMultiple( new FetchExpression(fetch2));
  58.  
    String name = "";
  59.  
     
  60.  
    foreach ( var c in result.Entities)
  61.  
    {
  62.  
    name += c.Attributes[ "name"];
  63.  
    }
  64.  
     
  65.  
    return name;
  66.  
    }
  67.  
    }
  68.  
    }

三.点击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,打开如下地址,出现以下界面则部署成功

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值