Dynamic CRM 2013 study notes (10) comparison of several data query methods on the client

Source: Internet
Author: User

We often need to query data on the client. Below are several common query methods: XMLHttpRequest, SDK. jquery, SDK. Rest.

XMLHttpRequest is the most basic call method. The jquery and rest methods are actually XMLHttpRequest, but they are encapsulated.

Jquery and rest have the same interface, so the call method is the same.

 

1. XMLHttpRequest

  • Definition
  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: }
 
  • Call method:
  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 is located under SDK \ samplecode \ JS \ restendpoint \ jqueryrestdataoperations \ scripts

 

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

 

  • Call method:
  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 is located under SDK \ samplecode \ JS \ restendpoint \ javascriptrestassociatedisassociate \ scripts

 

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

 

  • Call method:
  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 learning notes Series
 
 

Dynamic CRM 2013 study notes (10) comparison of several data query methods on the client

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.