Skip to main content

XMLHttpRequest vs Xrm.WebApi

 

XMLHttpRequest vs Xrm.WebApi

If you have written JavaScript code for Dynamics 365 you have seen the use of XMLHttpRequest or Xrm.WebApi to make server-side calls. The question is which one is the right one to use?

So, let’s analyze the two types of calls.

XMLHtttpRequest

1.       Synchronous

a.       Sample Code

var req = new XMLHttpRequest();

req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/accounts", false);

2.       Asynchronous

a.       Sample Code

var req = new XMLHttpRequest();

req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/accounts", true);

So, passing true parameter is going to make asynchronous call while passing it false is going to make a Synchronous call.

Xrm.WebApi

1.       Asynchronous

a.       There is no way to make a synchronous call, even if you are going to use chaining or await keyword it still going to be an asynchronous call.

 

In conclusion if you are looking for an asynchronous call you can use

1.       Xrm.WebApi

2.       XMLHttpRequest with true parameter

If you want to make Synchronous call, then your choice is quite simple use XRMHttpRequest.

 

Comments

  1. what about https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests#synchronous_request .Does this mean we have only the async method as an option now ?

    ReplyDelete
    Replies
    1. Bravo for WebApi calls for Dynamics 365 yes you have only async calls.
      for Synchronous calls you have to use XMLHtttpRequest

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Am I right in saying that under the hood of Xrm.WebApi you'd find XMLHttpRequest?

    So at the end of the day Xrm.WebApi is just a wrapper to:

    1) supposedly facilitate our calls
    2) standardize and apply best practices
    3) facilitate/centralize changes

    ReplyDelete

Post a Comment