Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AshV/2639c65018b703457b2fe26272093774 to your computer and use it in GitHub Desktop.
Save AshV/2639c65018b703457b2fe26272093774 to your computer and use it in GitHub Desktop.
Execute fetchXML with WebAPI in Dynamics 365
<!DOCTYPE html>
<html>
<head>
<title>Execute fetchXML with WebAPI in Dynamics 365 using JavaScript</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.3.0/milligram.min.css" integrity="sha256-Ro/wP8uUi8LR71kwIdilf78atpu8bTEwrK5ZotZo+Zc=" crossorigin="anonymous" />
<style> button { width:100% } </style>
</head>
<body>
<h1>Testing : Execute fetchXML with WebAPI in Dynamics 365 using JavaScript</h1>
<button onclick="GetXHR()" id="GetXHR">Test using GET with XMLHttpRequest</button>
<button onclick="GetFetch()" id="GetFetch">Test using GET with Fetch API</button>
<button onclick="PostXHR()" id="PostXHR">Test using POST with XMLHttpRequest</button>
<button onclick="PostFetch()" id="PostFetch">Test using POST with Fetch API</button>
<script>
var fetchXmlQuery = `
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="incident">
<attribute name="title" />
<attribute name="ticketnumber" />
<attribute name="createdon" />
<attribute name="incidentid" />
<attribute name="caseorigincode" />
<order attribute="title" descending="false" />
<filter type="and">
<condition attribute="statecode" operator="eq" value="0" />
</filter>
</entity>
</fetch>`;
var clientURL = location.protocol + '//' + location.host
function GetXHR() {
var req = new XMLHttpRequest();
req.open(
"GET",
clientURL +
"/api/data/v9.0/incidents?fetchXml=" +
encodeURIComponent(fetchXmlQuery),
true
);
req.setRequestHeader("Prefer", 'odata.include-annotations="*"');
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
console.dir(results);
alert(JSON.stringify(results));
} else {
alert(this.statusText);
}
}
};
req.send();
}
function GetFetch() {
fetch(
clientURL +
"/api/data/v9.0/incidents?fetchXml=" +
encodeURIComponent(fetchXmlQuery),
{
credentials: "same-origin",
headers: {
Prefer: 'odata.include-annotations="*"'
}
}
)
.then(response => response.json())
.then(result => { console.dir(result); alert(JSON.stringify(result)); })
.catch(error => console.error("Error:", error));
}
function PostXHR() {
var req = new XMLHttpRequest();
req.open(
"POST",
clientURL + "/api/data/v9.0/$batch",
true
);
req.setRequestHeader(
"Content-Type",
"multipart/mixed;boundary=batch_fetchquery"
);
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(
this.response.substring(
this.response.indexOf("{"),
this.response.lastIndexOf("}") + 1
)
);
console.dir(result);
alert(JSON.stringify(result));
} else {
console.error(this.statusText);
}
}
};
var body =
"--batch_fetchquery\n" +
"Content-Type: application/http\n" +
"Content-Transfer-Encoding: binary\n" +
"\n" +
"GET " +
clientURL +
"/api/data/v9.0/incidents?fetchXml=" +
encodeURIComponent(fetchXmlQuery) +
" HTTP/1.1\n" +
'Prefer: odata.include-annotations="*"\n' +
"\n" +
"--batch_fetchquery--";
req.send(body);
}
function PostFetch() {
fetch(clientURL + "/api/data/v9.0/$batch", {
body:
"--batch_fetchquery\n" +
"Content-Type: application/http\n" +
"Content-Transfer-Encoding: binary\n" +
"\n" +
"GET " +
clientURL +
"/api/data/v9.0/incidents?fetchXml=" +
encodeURIComponent(fetchXmlQuery) +
" HTTP/1.1\n" +
'Prefer: odata.include-annotations="*"\n' +
"\n" +
"--batch_fetchquery--",
headers: {
"Content-Type": "multipart/mixed;boundary=batch_fetchquery"
},
credentials: "same-origin",
method: "POST"
})
.then(response => response.text())
.then(data => {
var result = JSON.parse(
data.substring(data.indexOf("{"), data.lastIndexOf("}") + 1)
);
console.log(result);
alert(JSON.stringify(result));
})
.catch(error => console.error("Error:", error));
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment