Dynamics CRM WEB API Generic code
"use strict";
var ANK = window.ANK || {};
ANK.WebAPI = ANK.WebAPI || {};
(
function() {
this.Retrieve = function(entitySetName, filtersClause, requestHeader, asynch, successCallback, errorCallback) {
var req = new XMLHttpRequest();
var filterString = "";
if (filtersClause != null)
filterString = filtersClause;
var strURL = encodeURI(getWebAPIPath() + entitySetName) + filterString;
req.open("GET", strURL, asynch);
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.setRequestHeader("Prefer", 'odata.include-annotations="*"');
if (requestHeader != null && requestHeader != '')
setRequestHeader(req);
req.onreadystatechange = function() {
if (this.readyState == 4 /* complete */ ) {
req.onreadystatechange = null;
if (this.status == 200) {
var data = JSON.parse(this.response);
if (successCallback)
successCallback(data);
} else {
if (errorCallback)
errorCallback(ANK.WebAPI.errorHandler(this.response));
}
}
};
req.send();
},
this.UpdateSingleValue = function(entitySetName, PrimaryKeyValue, strPropertyName, objValues, asynch, successCallback, errorCallback) {
//convert JSON object to string
var body = JSON.stringify(objValues);
entitySetName += "(" + PrimaryKeyValue + ")";
if (strPropertyName == null || strPropertyName == '' || strPropertyName == undefined)
strPropertyName = '';
var req = new XMLHttpRequest();
req.open("PUT", encodeURI(getWebAPIPath() + entitySetName + "/" + strPropertyName), asynch);
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 == 204) {
var ObjectUri = this.getResponseHeader("OData-EntityId");
var ObjectID = '';
if (ObjectUri != null) {
//get EntityId from ResponseHeader of Created Record
ObjectID = ObjectUri.split(/[()]/);
ObjectID = ObjectID[1];
}
if (successCallback)
successCallback(ObjectID);
} else {
if (errorCallback)
errorCallback(ANK.WebAPI.errorHandler(this.response));
}
}
};
req.send(body);
},
this.Update = function(entitySetName, PrimaryKeyValue, objValues, asynch, successCallback, errorCallback) {
//convert JSON object to string
var body = JSON.stringify(objValues);
entitySetName += "(" + PrimaryKeyValue + ")";
var req = new XMLHttpRequest();
req.open("PATCH", encodeURI(getWebAPIPath() + entitySetName), asynch);
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 == 204) {
var ObjectUri = this.getResponseHeader("OData-EntityId");
//get EntityId from ResponseHeader of Created Record
var ObjectID = ObjectUri.split(/[()]/);
ObjectID = ObjectID[1];
if (successCallback)
successCallback(ObjectID);
} else {
if (errorCallback)
errorCallback(ANK.WebAPI.errorHandler(this.response));
}
}
};
req.send(body);
},
this.Delete = function(entitySetName, PrimaryKeyValue, asynch, successCallback, errorCallback) {
entitySetName += "(" + PrimaryKeyValue + ")";
var req = new XMLHttpRequest();
req.open("DELETE", encodeURI(getWebAPIPath() + entitySetName), asynch);
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 == 204) {
if (successCallback)
successCallback();
} else {
if (errorCallback)
errorCallback(ANK.WebAPI.errorHandler(this.response));
}
}
};
req.send();
},
this.Create = function(entitySetName, entity, successCallback, errorCallback) {
var req = new XMLHttpRequest();
req.open("POST", encodeURI(getWebAPIPath() + entitySetName), 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 == 204) {
if (successCallback)
successCallback(this.getResponseHeader("OData-EntityId"));
} else {
if (errorCallback)
errorCallback(MyNameSpace.WebAPI.errorHandler(this.response));
}
}
};
req.send(JSON.stringify(entity));
};
function setRequestHeader(req) {
for (var key in headers) {
req.setRequestHeader(key, headers[key]);
}
}
//Internal supporting functions
function getClientUrl() {
//Get the organization URL
if (typeof GetGlobalContext == "function" &&
typeof GetGlobalContext().getClientUrl == "function") {
return GetGlobalContext().getClientUrl();
} else {
//If GetGlobalContext is not defined check for Xrm.Page.context;
if (typeof Xrm != "undefined" &&
typeof Xrm.Page != "undefined" &&
typeof Xrm.Page.context != "undefined" &&
typeof Xrm.Page.context.getClientUrl == "function") {
try {
return Xrm.Page.context.getClientUrl();
} catch (e) {
throw new Error("Xrm.Page.context.getClientUrl is not available.");
}
} else if (typeof parent.Xrm != "undefined" &&
typeof parent.Xrm.Page != "undefined" &&
typeof parent.Xrm.Page.context != "undefined" &&
typeof parent.Xrm.Page.context.getClientUrl == "function") {
try {
return parent.Xrm.Page.context.getClientUrl();
} catch (e) {
throw new Error("parent.Xrm.Page.context.getClientUrl is not available.");
}
} else {
throw new Error("Context is not available.");
}
}
}
function getWebAPIPath() {
return getClientUrl() + "/api/data/v8.0/";
}
// This function is called when an error callback parses the JSON response
// It is a public function because the error callback occurs within the onreadystatechange
// event handler and an internal function would not be in scope.
this.errorHandler = function(resp) {
try {
return JSON.parse(resp).error;
} catch (e) {
return new Error("Unexpected Error")
}
}
}).call(ANK.WebAPI);
"use strict";
var ANK = window.ANK || {};
ANK.WebAPI = ANK.WebAPI || {};
(
function() {
this.Retrieve = function(entitySetName, filtersClause, requestHeader, asynch, successCallback, errorCallback) {
var req = new XMLHttpRequest();
var filterString = "";
if (filtersClause != null)
filterString = filtersClause;
var strURL = encodeURI(getWebAPIPath() + entitySetName) + filterString;
req.open("GET", strURL, asynch);
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.setRequestHeader("Prefer", 'odata.include-annotations="*"');
if (requestHeader != null && requestHeader != '')
setRequestHeader(req);
req.onreadystatechange = function() {
if (this.readyState == 4 /* complete */ ) {
req.onreadystatechange = null;
if (this.status == 200) {
var data = JSON.parse(this.response);
if (successCallback)
successCallback(data);
} else {
if (errorCallback)
errorCallback(ANK.WebAPI.errorHandler(this.response));
}
}
};
req.send();
},
this.UpdateSingleValue = function(entitySetName, PrimaryKeyValue, strPropertyName, objValues, asynch, successCallback, errorCallback) {
//convert JSON object to string
var body = JSON.stringify(objValues);
entitySetName += "(" + PrimaryKeyValue + ")";
if (strPropertyName == null || strPropertyName == '' || strPropertyName == undefined)
strPropertyName = '';
var req = new XMLHttpRequest();
req.open("PUT", encodeURI(getWebAPIPath() + entitySetName + "/" + strPropertyName), asynch);
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 == 204) {
var ObjectUri = this.getResponseHeader("OData-EntityId");
var ObjectID = '';
if (ObjectUri != null) {
//get EntityId from ResponseHeader of Created Record
ObjectID = ObjectUri.split(/[()]/);
ObjectID = ObjectID[1];
}
if (successCallback)
successCallback(ObjectID);
} else {
if (errorCallback)
errorCallback(ANK.WebAPI.errorHandler(this.response));
}
}
};
req.send(body);
},
this.Update = function(entitySetName, PrimaryKeyValue, objValues, asynch, successCallback, errorCallback) {
//convert JSON object to string
var body = JSON.stringify(objValues);
entitySetName += "(" + PrimaryKeyValue + ")";
var req = new XMLHttpRequest();
req.open("PATCH", encodeURI(getWebAPIPath() + entitySetName), asynch);
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 == 204) {
var ObjectUri = this.getResponseHeader("OData-EntityId");
//get EntityId from ResponseHeader of Created Record
var ObjectID = ObjectUri.split(/[()]/);
ObjectID = ObjectID[1];
if (successCallback)
successCallback(ObjectID);
} else {
if (errorCallback)
errorCallback(ANK.WebAPI.errorHandler(this.response));
}
}
};
req.send(body);
},
this.Delete = function(entitySetName, PrimaryKeyValue, asynch, successCallback, errorCallback) {
entitySetName += "(" + PrimaryKeyValue + ")";
var req = new XMLHttpRequest();
req.open("DELETE", encodeURI(getWebAPIPath() + entitySetName), asynch);
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 == 204) {
if (successCallback)
successCallback();
} else {
if (errorCallback)
errorCallback(ANK.WebAPI.errorHandler(this.response));
}
}
};
req.send();
},
this.Create = function(entitySetName, entity, successCallback, errorCallback) {
var req = new XMLHttpRequest();
req.open("POST", encodeURI(getWebAPIPath() + entitySetName), 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 == 204) {
if (successCallback)
successCallback(this.getResponseHeader("OData-EntityId"));
} else {
if (errorCallback)
errorCallback(MyNameSpace.WebAPI.errorHandler(this.response));
}
}
};
req.send(JSON.stringify(entity));
};
function setRequestHeader(req) {
for (var key in headers) {
req.setRequestHeader(key, headers[key]);
}
}
//Internal supporting functions
function getClientUrl() {
//Get the organization URL
if (typeof GetGlobalContext == "function" &&
typeof GetGlobalContext().getClientUrl == "function") {
return GetGlobalContext().getClientUrl();
} else {
//If GetGlobalContext is not defined check for Xrm.Page.context;
if (typeof Xrm != "undefined" &&
typeof Xrm.Page != "undefined" &&
typeof Xrm.Page.context != "undefined" &&
typeof Xrm.Page.context.getClientUrl == "function") {
try {
return Xrm.Page.context.getClientUrl();
} catch (e) {
throw new Error("Xrm.Page.context.getClientUrl is not available.");
}
} else if (typeof parent.Xrm != "undefined" &&
typeof parent.Xrm.Page != "undefined" &&
typeof parent.Xrm.Page.context != "undefined" &&
typeof parent.Xrm.Page.context.getClientUrl == "function") {
try {
return parent.Xrm.Page.context.getClientUrl();
} catch (e) {
throw new Error("parent.Xrm.Page.context.getClientUrl is not available.");
}
} else {
throw new Error("Context is not available.");
}
}
}
function getWebAPIPath() {
return getClientUrl() + "/api/data/v8.0/";
}
// This function is called when an error callback parses the JSON response
// It is a public function because the error callback occurs within the onreadystatechange
// event handler and an internal function would not be in scope.
this.errorHandler = function(resp) {
try {
return JSON.parse(resp).error;
} catch (e) {
return new Error("Unexpected Error")
}
}
}).call(ANK.WebAPI);
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.