Dynamic CRM 2013学习笔记(十)客户端几种查询数据方式比较

我们经常要在客户端进行数据查询,下面分别比较常用的几种查询方式:XMLHttpRequest, SDK.JQuery, SDK.Rest.

XMLHttpRequest是最基本的调用方式,JQuery和Rest的二种方式其实也是用的XMLHttpRequest,只不过是把它封装了一下

JQuery和Rest二种方式的接口一样,所以调用方式也一样

 

1. XMLHttpRequest

  • 定义
 
 
  1: function ODataRetrieve(oDataString) {
 
 
  2:     var ServerUrl = Xrm.Page.context.getClientUrl();
 
 
  3:     if (ServerUrl.match(/\/$/)) {
 
 
  4:         ServerUrl = ServerUrl.substring(0, ServerUrl.length - 1);
 
 
  5:     }
 
 
  6:     var retrieveReq = new XMLHttpRequest();
 
 
  7:     retrieveReq.open("GET", ServerUrl + "/XRMServices/2011/OrganizationData.svc/" + oDataString, false);
 
 
  8:     retrieveReq.setRequestHeader("Accept", "application/json");
 
 
  9:     retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
 
 
 10:     retrieveReq.send();
 
 
 11:     return JSON.parse(retrieveReq.responseText).d;
 
 
 12: }
 
  • 调用方法:
 
 
  1: var retrievedContact = ODataRetrieve("ContactSet(guid'" + ContactAttribute.getValue()[0].id + "')?Select=JobTitle,MobilePhone");
 
 
  2:     if (retrievedContact != null) {
 
 
  3:         ContactJobAttribute.setValue(retrievedContact.JobTitle);
 
 
  4:         ContactPhoneAttribute.setValue(retrievedContact.MobilePhone);
 
 
  5: }
 
2. SDK.JQuery
  • SDK.JQuery.js 位于SDK\SampleCode\JS\RESTEndpoint\JQueryRESTDataOperations\JQueryRESTDataOperations\Scripts下

 

 
 
  1: retrieveRecord: function (id, type, select, expand, successCallback, errorCallback)
  2: retrieveMultipleRecords: function (type, options, successCallback, errorCallback, OnComplete)

 

  • 调用方法:
 
 
  1: SDK.JQuery.retrieveRecord(
 
 
  2:      AccountId,
 
 
  3:      "Account",
 
 
  4:      null, null,
 
 
  5:      function (account) {
 
 
  6:       writeMessage("Retrieved the account named \"" + account.Name + "\". This account was created on : \"" + account.CreatedOn + "\".");
 
 
  7:       updateAccount(AccountId);
 
 
  8:      },
 
 
  9:      errorHandler
 
 
 10:    );
 
 
 11: 
 
 
 
  1: SDK.JQuery.retrieveMultipleRecords(
  2:      "Contact",
  3:      "$select=ContactId,FullName&$top=1",
  4:      function (results) {
  5:       var firstResult = results[0];
  6:       if (firstResult != null) {
  7:        primaryContact = results[0];
  8:       }
  9:       else {
 10:        writeMessage("No Contact records are available to set as the primary contact for the account.");
 11:       }
 12:      },
 13:      errorHandler,
 14:      function () {
 15:       //OnComplete handler
 16:      }
 17:    );
 18: 

 

3. SDK.Rest

  • SDK.Rest.js 位于SDK\SampleCode\JS\RESTEndpoint\JavaScriptRESTAssociateDisassociate\JavaScriptRESTAssociateDisassociate\Scripts下

 

 
 
 
 
  1: retrieveRecord: function (id, type, select, expand, successCallback, errorCallback)
  2: retrieveMultipleRecords: function (type, options, successCallback, errorCallback, OnComplete)

 

  • 调用方法:
 
 
  1:  SDK.REST.retrieveRecord(
 
 
  2:      AccountId,
 
 
  3:      "Account",
 
 
  4:      null,null,
 
 
  5:      function (account) {
 
 
  6:       writeMessage("Retrieved the account named \"" + account.Name + "\". This account was created on : \"" + account.CreatedOn + "\".");
 
 
  7:       updateAccount(AccountId);
 
 
  8:      },
 
 
  9:      errorHandler
 
 
 10:    );
 
 
 11: 
 
 
  1: SDK.REST.retrieveMultipleRecords(
  2:      "Contact",
  3:      "$select=ContactId,FullName&$top=1",
  4:      function (results) {
  5:       var firstResult = results[0];
  6:       if (firstResult != null) {
  7:        primaryContact = results[0];
  8:       }
  9:       else {
 10:        writeMessage("No Contact records are available to set as the primary contact for the account.");
 11:       }
 12:      },
 13:      errorHandler,
 14:      function () { 
 15:      //OnComplete handler
 16:       }
 17:    );
 18: 
 
Dynamic CRM 2013学习笔记 系列汇总
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值