Voice for Dynamics exposes a JavaScript API which can be called by any custom or out-of-the-box JS plugin to carry out certain tasks. All functions within the API are asynchronous and the proper way to call is to use await
statement or properly handle the Promise
object returned.
Enreach.API.executeQuery
Sends a request to Enreach REST API. Authentication is managed automatically, by adding the token to request header.
Parameters:
query
(string): path within Enreach APIpostData
(object): Object which gets sent as payload to Enreach API in a POST or PUT requestmethod
(string): HTTP method to be used. If specified, overrides the method determined by the existence ofpostData
;
...
If postData
parameter is NULL then a GET request is sent, whereas any object passed here means a POST request. The object passed here is serialized as JSON in the request payload.
...
The purpose and usage of the method is identical to the underlying API provided by Microsoft, the only addition is the retry logic.
Enreach.API.Xrm.execute
Executes a request (e.g. an action) against Dynamics API.
Parameters:
data
: the data (payload) to be sent to the APImetaData
: the metadata of the request
This API function utilizes the same window.postMessage()
mechanism as all the rest of the Xrm API functions.
In case of Dynamics WebAPI calls, metadata retrieval happens by adding a getMetadata()
function to the prototype of the request function. The parameters (data
and metaData
) are serialized to JSON and then sent to a sub-page for processing using window.postMessage()
. Since functions like getMetadata couldn’t be serialized, data
and metaData
have to be passed separately, and the final query object is assembled within the sub-page handling the request and response.
Example code to execute a WhoAmIRequest in a custom plugin:
Code Block | ||
---|---|---|
| ||
var SampleCustomer = SampleCustomer || {}; SampleCustomer.Plugins = SampleCustomer.Plugins || {}; SampleCustomer.Plugins.whoAmI = async function (context) { var requestData = {}; var metadata = { boundParameter: null, parameterTypes: {}, operationType: 1, // This is a function. Use '0' for actions and '2' for CRUD operationName: "WhoAmI", }; var response = await Enreach.API.Xrm.execute(requestData, metadata); console.log("Your user id is " + response.UserId); }; |