Created
September 26, 2017 04:20
-
-
Save sbedford/15caa4efd8b5ebb36ae05e08c782cfa5 to your computer and use it in GitHub Desktop.
Javascript to interact with Dynamics 365 SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Sdk = window.Sdk || {} | |
Sdk.request = function (action, uri, data, formattedValue, maxPageSize) { | |
if (!RegExp(action, "g").test("POST PATCH PUT GET DELETE")) { // Expected action verbs. | |
throw new Error("Sdk.request: action parameter must be one of the following: " + | |
"POST, PATCH, PUT, GET, or DELETE."); | |
} | |
if (!typeof uri === "string") { | |
throw new Error("Sdk.request: uri parameter must be a string."); | |
} | |
if ((RegExp(action, "g").test("POST PATCH PUT")) && (data === null || data === undefined)) { | |
throw new Error("Sdk.request: data parameter must not be null for operations that create or modify data."); | |
} | |
if (maxPageSize === null || maxPageSize === undefined) { | |
maxPageSize = 10; // Default limit is 10 entities per page. | |
} | |
// Construct a fully qualified URI if a relative URI is passed in. | |
if (uri.charAt(0) === "/") { | |
uri = clientUrl + webAPIPath + uri; | |
} | |
return new Promise(function (resolve, reject) { | |
var request = new XMLHttpRequest(); | |
request.open(action, encodeURI(uri), true); | |
request.setRequestHeader("OData-MaxVersion", "4.0"); | |
request.setRequestHeader("OData-Version", "4.0"); | |
request.setRequestHeader("Accept", "application/json"); | |
request.setRequestHeader("Content-Type", "application/json; charset=utf-8"); | |
request.setRequestHeader("Prefer", "odata.maxpagesize=" + maxPageSize); | |
if (formattedValue) { | |
request.setRequestHeader("Prefer", | |
"odata.include-annotations=OData.Community.Display.V1.FormattedValue"); | |
} | |
request.onreadystatechange = function () { | |
if (this.readyState === 4) { | |
request.onreadystatechange = null; | |
switch (this.status) { | |
case 200: // Success with content returned in response body. | |
case 204: // Success with no content returned in response body. | |
resolve(this); | |
break; | |
default: // All other statuses are unexpected so are treated like errors. | |
var error; | |
try { | |
error = JSON.parse(request.response).error; | |
} catch (e) { | |
error = new Error("Unexpected Error"); | |
} | |
reject(error); | |
break; | |
} | |
} | |
}; | |
request.send(JSON.stringify(data)); | |
}); | |
}; | |
var url = Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts?$filter=firstname eq 'Sean'&$select=fullname"; | |
Sdk.request("GET", url, null, true) | |
.then(function (request) { | |
var collection = JSON.parse(request.response).value; | |
for(var i=0;i<collection.length;i++){ | |
console.log(collection[i]["fullname"]); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment