JScript to retrieve and execute view (Predefined Query) using Web API – CRM 2016
Assume you would need to retrieve ‘Active Accounts’ from your script, you can either retrieve by building FetchXML from scratch or simply use Web API to execute existing ‘Active Accounts’ view query with no need of writing Fetch XML.
Using Web API we can retrieve and execute predefined queries (i.e., System view and Personal view queries).
Below are the steps to retrieve and execute ‘System view’ query using CRM WebAPI
- Get the GUID of View by Name.
- Execute the view and fetch the result set. (Columns of the result set would be same as ‘System View’ columns)
Script
function executeView(viewName) {
try {
var clientUrl = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest();
// Get ‘Active Accounts’ GUID
req.open(“GET”, encodeURI(clientUrl + “/api/data/v8.0/savedqueries?$select=name,savedqueryid&$filter=name eq ‘” + viewName + “‘”), true);
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null;
if (this.status == 200) {
var data = JSON.parse(this.response);
var dat = data.value;
for (var i = 0; i < dat.length; i++) {
var viewId = dat[i].savedqueryid;
alert(“View Id : ” + viewId);
if (viewId) {
executeViewQuery(viewId);
}
}
}
else {
var error = JSON.parse(this.response).error;
alert(“Error while fetching view id” + error.message);
}
}};
req.send();
} catch (e) {
alert(“Error in getViewId” + e.description);
}
}
function executeViewQuery(viewId) {
try {
var clientUrl = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest()
req.open(“GET”, encodeURI(clientUrl + “/api/data/v8.0/accounts?savedQuery=” + viewId + “”), true);
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
// Include this to get Lookup and Optionset text
req.setRequestHeader(“Prefer”, “odata.include-annotations=OData.Community.Display.V1.FormattedValue”);
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null;
if (this.status == 200) {
var data = JSON.parse(this.response);
var dat = data.value;
for (var i = 0; i < dat.length; i++) {
alert(“Account Name – ” + dat[i].name);
// Read Lookup name (i.e.,Primary Contact)
alert(“Primary Contact – ” + dat[i][‘_primarycontactid_value@OData.Community.Display.V1.FormattedValue’]);
}
}
else {
var error = JSON.parse(this.response).error;
alert(“Error while executing view query” + error.message);
}
}
};
req.send();
} catch (e) {
alert(“Error in getViewId” + e.description);
}
}
The result set would look as below.
How Do I call this method:
- Copy and paste above two functions.
- Pass the ‘System View’ name to executeView({View Name}) function.
// To Retrieve and Execute ‘Active Accounts’ view
executeView(“Active Accounts”);
Note: To use “Personal View”, change the “savedqueries” to “userqueries” in Wab API URI.
Refer MSDN Article for more details.
🙂