JQueryRESTDataOperations.
// <snippetJQueryRESTDataOperations.Functions>
/// <reference path="jquery1.4.1vsdoc.js" />
//GetGlobalContext function exists in ClientGlobalContext.js.aspx so the
//host HTML page must have a reference to ClientGlobalContext.js.aspx.
//Retrieve the server url, which differs on-premise from on-line and
//shouldn't be hard-coded.
//The XRM OData end-point
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
function serverUrl() {
ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
}
function getServerUrl() {///<summary>
/// Private function used to establish the path to the SOAP endpoint based on context
/// provided by the Xrm.Page object or the context object returned by the GlobalContext object.
///</summary>
var url = parent.Xrm.Page.context.getClientUrl();
if (url.match(/\/$/)) {
url = url.substring(0, url.length - 1);
}
var locationhref = location.href;
if (locationhref.indexOf(url) == -1) {
var serverIndex = url.lastIndexOf("/");
var serverUrl = url.substring(0, serverIndex);
url = url.replace(serverUrl, locationhref.substring(0, locationhref.indexOf(url.substring(serverIndex))));
}
return url;
}
///<summary>
///Uses jQuery's AJAX object to call the Microsoft Dynamics CRM OData endpoint to
/// Create a new record
///</summary>
/// <param name="entityObject" type="Object" required="true">
///1: entity - a loose-type object representing an OData entity. any fields
/// on this object must be camel-cased and named exactly as they
/// appear in entity metadata
///</param>
/// <param name="odataSetName" type="string" required="true">
///1: set - a string representing an OData Set. OData provides uri access
/// to any CRM entity collection. examples: AccountSet, ContactSet,
/// OpportunitySet.
///</param>
/// <param name="successCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon success
/// of the ajax invocation.
///</param>
/// <param name="errorCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon error
/// of the ajax invocation.
///</param>
function createRecord(entityObject, odataSetName, successCallback, errorCallback) {
//entityObject is required
if (!entityObject) {
alert("entityObject is required.");
return;
}
//odataSetName is required, i.e. "AccountSet"
if (!odataSetName) {
alert("odataSetName is required.");
return;
}
//Parse the entity object into JSON
var jsonEntity = window.JSON.stringify(entityObject);
//Asynchronous AJAX function to Create a CRM record using OData
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: getServerUrl() + ODATA_ENDPOINT + "/" + odataSetName,
data: jsonEntity,
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
if (successCallback) {
successCallback(data.d, textStatus, XmlHttpRequest);
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (errorCallback) {
errorCallback(XmlHttpRequest, textStatus, errorThrown);
} else {
errorHandler(XmlHttpRequest, textStatus, errorThrown);
}
}
});
}
///<summary>
///Uses jQuery's AJAX object to call the Microsoft Dynamics CRM OData endpoint to
/// retrieve an existing record
///</summary>
/// <param name="id" type="guid" required="true">
///1: id - the guid (primarykey) of the record to be retrieved
///</param>
/// <param name="odataSetName" type="string" required="true">
///1: set - a string representing an OData Set. OData provides uri access
/// to any CRM entity collection. examples: AccountSet, ContactSet,
/// OpportunitySet.
///</param>
/// <param name="successCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon success
/// of the ajax invocation.
///</param>
/// <param name="errorCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon error
/// of the ajax invocation.
///</param>
function retrieveRecord(id, odataSetName, successCallback, errorCallback) {
//id is required
if (!id) {
alert("record id is required.");
return;
}
//odataSetName is required, i.e. "AccountSet"
if (!odataSetName) {
alert("odataSetName is required.");
return;
}
//Asynchronous AJAX function to Retrieve a CRM record using OData
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: getServerUrl() + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + id + "')",
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
if (successCallback) {
successCallback(data.d, textStatus, XmlHttpRequest);
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (errorCallback) {
errorCallback(XmlHttpRequest, textStatus, errorThrown);
} else {
errorHandler(XmlHttpRequest, textStatus, errorThrown);
}
}
});
}
///<summary>
///Uses jQuery's AJAX object to call the Microsoft Dynamics CRM OData endpoint to
/// Retrieve multiple records
///</summary>
/// <param name="odataSetName" type="string" required="true">
///1: set - a string representing an OData Set. OData provides uri access
/// to any CRM entity collection. examples: AccountSet, ContactSet,
/// OpportunitySet.
///</param>
/// <param name="filter" type="string">
///1: filter - a string representing the filter that is appended to the odatasetname
/// of the OData URI.
///</param>
/// <param name="successCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon success
/// of the ajax invocation.
///</param>
/// <param name="errorCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon error
/// of the ajax invocation.
///</param>
function retrieveMultiple(odataSetName, filter, successCallback, errorCallback) {
//odataSetName is required, i.e. "AccountSet"
if (!odataSetName) {
alert("odataSetName is required.");
return;
}
//Build the URI
var odataUri = getServerUrl() + ODATA_ENDPOINT + "/" + odataSetName + "()";
//If a filter is supplied, append it to the OData URI
if (filter) {
odataUri += filter;
}
//Asynchronous AJAX function to Retrieve CRM records using OData
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: odataUri,
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
if (successCallback) {
if (data && data.d && data.d.results) {
successCallback(data.d.results, textStatus, XmlHttpRequest);
} else if (data && data.d) {
successCallback(data.d, textStatus, XmlHttpRequest);
} else {
successCallback(data, textStatus, XmlHttpRequest);
}
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (errorCallback) {
errorCallback(XmlHttpRequest, textStatus, errorThrown);
} else {
errorHandler(XmlHttpRequest, textStatus, errorThrown);
}
}
});
}
///<summary>
///Uses jQuery's AJAX object to call the Microsoft Dynamics CRM OData endpoint to
/// update an existing record
///</summary>
/// <param name="id" type="guid" required="true">
///1: id - the guid (primarykey) of the record to be retrieved
///</param>
/// <param name="entityObject" type="Object" required="true">
///1: entity - a loose-type object representing an OData entity. any fields
/// on this object must be camel-cased and named exactly as they
/// appear in entity metadata
///</param>
/// <param name="odataSetName" type="string" required="true">
///1: set - a string representing an OData Set. OData provides uri access
/// to any CRM entity collection. examples: AccountSet, ContactSet,
/// OpportunitySet.
///</param>
/// <param name="successCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon success
/// of the ajax invocation.
///</param>
/// <param name="errorCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon error
/// of the ajax invocation.
///</param>
function updateRecord(id, entityObject, odataSetName, successCallback, errorCallback) {
//id is required
if (!id) {
alert("record id is required.");
return;
}
//odataSetName is required, i.e. "AccountSet"
if (!odataSetName) {
alert("odataSetName is required.");
return;
}
//Parse the entity object into JSON
var jsonEntity = window.JSON.stringify(entityObject);
//Asynchronous AJAX function to Update a CRM record using OData
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
data: jsonEntity,
url: getServerUrl() + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + id + "')",
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
//Specify the HTTP method MERGE to update just the changes you are submitting.
XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
},
success: function (data, textStatus, XmlHttpRequest) {
//The MERGE does not return any data at all, so we'll add the id
//onto the data object so it can be leveraged in a Callback. When data
//is used in the callback function, the field will be named generically, "id"
data = {};
data.id = id;
if (successCallback) {
successCallback(data, textStatus, XmlHttpRequest);
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (errorCallback) {
errorCallback(XmlHttpRequest, textStatus, errorThrown);
} else {
errorHandler(XmlHttpRequest, textStatus, errorThrown);
}
}
});
}
///<summary>
///Uses jQuery's AJAX object to call the Microsoft Dynamics CRM OData endpoint to
/// delete an existing record
///</summary>
/// <param name="id" type="guid" required="true">
///1: id - the guid (primarykey) of the record to be retrieved
///</param>
/// <param name="odataSetName" type="string" required="true">
///1: set - a string representing an OData Set. OData provides uri access
/// to any CRM entity collection. examples: AccountSet, ContactSet,
/// OpportunitySet.
///</param>
/// <param name="successCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon success
/// of the ajax invocation.
///</param>
/// <param name="errorCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon error
/// of the ajax invocation.
///</param>
function deleteRecord(id, odataSetName, successCallback, errorCallback) {
//id is required
if (!id) {
alert("record id is required.");
return;
}
//odataSetName is required, i.e. "AccountSet"
if (!odataSetName) {
alert("odataSetName is required.");
return;
}
//Asynchronous AJAX function to Delete a CRM record using OData
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: getServerUrl() + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + id + "')",
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
//Specify the HTTP method DELETE to perform a delete operation.
XMLHttpRequest.setRequestHeader("X-HTTP-Method", "DELETE");
},
success: function (data, textStatus, XmlHttpRequest) {
if (successCallback) {
successCallback(data.d, textStatus, XmlHttpRequest);
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (errorCallback) {
errorCallback(XmlHttpRequest, textStatus, errorThrown);
} else {
errorHandler(XmlHttpRequest, textStatus, errorThrown);
}
}
});
}
///<summary>
///A function that will display the error results of an AJAX operation
///</summary>
function errorHandler(xmlHttpRequest, textStatus, errorThrow) {
alert("Error : " + textStatus + ": " + xmlHttpRequest.statusText);
}
// </snippetJQueryRESTDataOperations.Functions>
/// <reference path="jquery1.4.1vsdoc.js" />
//GetGlobalContext function exists in ClientGlobalContext.js.aspx so the
//host HTML page must have a reference to ClientGlobalContext.js.aspx.
//Retrieve the server url, which differs on-premise from on-line and
//shouldn't be hard-coded.
//The XRM OData end-point
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
function serverUrl() {
ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
}
function getServerUrl() {///<summary>
/// Private function used to establish the path to the SOAP endpoint based on context
/// provided by the Xrm.Page object or the context object returned by the GlobalContext object.
///</summary>
var url = parent.Xrm.Page.context.getClientUrl();
if (url.match(/\/$/)) {
url = url.substring(0, url.length - 1);
}
var locationhref = location.href;
if (locationhref.indexOf(url) == -1) {
var serverIndex = url.lastIndexOf("/");
var serverUrl = url.substring(0, serverIndex);
url = url.replace(serverUrl, locationhref.substring(0, locationhref.indexOf(url.substring(serverIndex))));
}
return url;
}
///<summary>
///Uses jQuery's AJAX object to call the Microsoft Dynamics CRM OData endpoint to
/// Create a new record
///</summary>
/// <param name="entityObject" type="Object" required="true">
///1: entity - a loose-type object representing an OData entity. any fields
/// on this object must be camel-cased and named exactly as they
/// appear in entity metadata
///</param>
/// <param name="odataSetName" type="string" required="true">
///1: set - a string representing an OData Set. OData provides uri access
/// to any CRM entity collection. examples: AccountSet, ContactSet,
/// OpportunitySet.
///</param>
/// <param name="successCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon success
/// of the ajax invocation.
///</param>
/// <param name="errorCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon error
/// of the ajax invocation.
///</param>
function createRecord(entityObject, odataSetName, successCallback, errorCallback) {
//entityObject is required
if (!entityObject) {
alert("entityObject is required.");
return;
}
//odataSetName is required, i.e. "AccountSet"
if (!odataSetName) {
alert("odataSetName is required.");
return;
}
//Parse the entity object into JSON
var jsonEntity = window.JSON.stringify(entityObject);
//Asynchronous AJAX function to Create a CRM record using OData
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: getServerUrl() + ODATA_ENDPOINT + "/" + odataSetName,
data: jsonEntity,
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
if (successCallback) {
successCallback(data.d, textStatus, XmlHttpRequest);
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (errorCallback) {
errorCallback(XmlHttpRequest, textStatus, errorThrown);
} else {
errorHandler(XmlHttpRequest, textStatus, errorThrown);
}
}
});
}
///<summary>
///Uses jQuery's AJAX object to call the Microsoft Dynamics CRM OData endpoint to
/// retrieve an existing record
///</summary>
/// <param name="id" type="guid" required="true">
///1: id - the guid (primarykey) of the record to be retrieved
///</param>
/// <param name="odataSetName" type="string" required="true">
///1: set - a string representing an OData Set. OData provides uri access
/// to any CRM entity collection. examples: AccountSet, ContactSet,
/// OpportunitySet.
///</param>
/// <param name="successCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon success
/// of the ajax invocation.
///</param>
/// <param name="errorCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon error
/// of the ajax invocation.
///</param>
function retrieveRecord(id, odataSetName, successCallback, errorCallback) {
//id is required
if (!id) {
alert("record id is required.");
return;
}
//odataSetName is required, i.e. "AccountSet"
if (!odataSetName) {
alert("odataSetName is required.");
return;
}
//Asynchronous AJAX function to Retrieve a CRM record using OData
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: getServerUrl() + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + id + "')",
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
if (successCallback) {
successCallback(data.d, textStatus, XmlHttpRequest);
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (errorCallback) {
errorCallback(XmlHttpRequest, textStatus, errorThrown);
} else {
errorHandler(XmlHttpRequest, textStatus, errorThrown);
}
}
});
}
///<summary>
///Uses jQuery's AJAX object to call the Microsoft Dynamics CRM OData endpoint to
/// Retrieve multiple records
///</summary>
/// <param name="odataSetName" type="string" required="true">
///1: set - a string representing an OData Set. OData provides uri access
/// to any CRM entity collection. examples: AccountSet, ContactSet,
/// OpportunitySet.
///</param>
/// <param name="filter" type="string">
///1: filter - a string representing the filter that is appended to the odatasetname
/// of the OData URI.
///</param>
/// <param name="successCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon success
/// of the ajax invocation.
///</param>
/// <param name="errorCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon error
/// of the ajax invocation.
///</param>
function retrieveMultiple(odataSetName, filter, successCallback, errorCallback) {
//odataSetName is required, i.e. "AccountSet"
if (!odataSetName) {
alert("odataSetName is required.");
return;
}
//Build the URI
var odataUri = getServerUrl() + ODATA_ENDPOINT + "/" + odataSetName + "()";
//If a filter is supplied, append it to the OData URI
if (filter) {
odataUri += filter;
}
//Asynchronous AJAX function to Retrieve CRM records using OData
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: odataUri,
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
if (successCallback) {
if (data && data.d && data.d.results) {
successCallback(data.d.results, textStatus, XmlHttpRequest);
} else if (data && data.d) {
successCallback(data.d, textStatus, XmlHttpRequest);
} else {
successCallback(data, textStatus, XmlHttpRequest);
}
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (errorCallback) {
errorCallback(XmlHttpRequest, textStatus, errorThrown);
} else {
errorHandler(XmlHttpRequest, textStatus, errorThrown);
}
}
});
}
///<summary>
///Uses jQuery's AJAX object to call the Microsoft Dynamics CRM OData endpoint to
/// update an existing record
///</summary>
/// <param name="id" type="guid" required="true">
///1: id - the guid (primarykey) of the record to be retrieved
///</param>
/// <param name="entityObject" type="Object" required="true">
///1: entity - a loose-type object representing an OData entity. any fields
/// on this object must be camel-cased and named exactly as they
/// appear in entity metadata
///</param>
/// <param name="odataSetName" type="string" required="true">
///1: set - a string representing an OData Set. OData provides uri access
/// to any CRM entity collection. examples: AccountSet, ContactSet,
/// OpportunitySet.
///</param>
/// <param name="successCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon success
/// of the ajax invocation.
///</param>
/// <param name="errorCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon error
/// of the ajax invocation.
///</param>
function updateRecord(id, entityObject, odataSetName, successCallback, errorCallback) {
//id is required
if (!id) {
alert("record id is required.");
return;
}
//odataSetName is required, i.e. "AccountSet"
if (!odataSetName) {
alert("odataSetName is required.");
return;
}
//Parse the entity object into JSON
var jsonEntity = window.JSON.stringify(entityObject);
//Asynchronous AJAX function to Update a CRM record using OData
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
data: jsonEntity,
url: getServerUrl() + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + id + "')",
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
//Specify the HTTP method MERGE to update just the changes you are submitting.
XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
},
success: function (data, textStatus, XmlHttpRequest) {
//The MERGE does not return any data at all, so we'll add the id
//onto the data object so it can be leveraged in a Callback. When data
//is used in the callback function, the field will be named generically, "id"
data = {};
data.id = id;
if (successCallback) {
successCallback(data, textStatus, XmlHttpRequest);
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (errorCallback) {
errorCallback(XmlHttpRequest, textStatus, errorThrown);
} else {
errorHandler(XmlHttpRequest, textStatus, errorThrown);
}
}
});
}
///<summary>
///Uses jQuery's AJAX object to call the Microsoft Dynamics CRM OData endpoint to
/// delete an existing record
///</summary>
/// <param name="id" type="guid" required="true">
///1: id - the guid (primarykey) of the record to be retrieved
///</param>
/// <param name="odataSetName" type="string" required="true">
///1: set - a string representing an OData Set. OData provides uri access
/// to any CRM entity collection. examples: AccountSet, ContactSet,
/// OpportunitySet.
///</param>
/// <param name="successCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon success
/// of the ajax invocation.
///</param>
/// <param name="errorCallback" type="function" >
///1: callback-a function that can be supplied as a callback upon error
/// of the ajax invocation.
///</param>
function deleteRecord(id, odataSetName, successCallback, errorCallback) {
//id is required
if (!id) {
alert("record id is required.");
return;
}
//odataSetName is required, i.e. "AccountSet"
if (!odataSetName) {
alert("odataSetName is required.");
return;
}
//Asynchronous AJAX function to Delete a CRM record using OData
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: getServerUrl() + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + id + "')",
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
//Specify the HTTP method DELETE to perform a delete operation.
XMLHttpRequest.setRequestHeader("X-HTTP-Method", "DELETE");
},
success: function (data, textStatus, XmlHttpRequest) {
if (successCallback) {
successCallback(data.d, textStatus, XmlHttpRequest);
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (errorCallback) {
errorCallback(XmlHttpRequest, textStatus, errorThrown);
} else {
errorHandler(XmlHttpRequest, textStatus, errorThrown);
}
}
});
}
///<summary>
///A function that will display the error results of an AJAX operation
///</summary>
function errorHandler(xmlHttpRequest, textStatus, errorThrow) {
alert("Error : " + textStatus + ": " + xmlHttpRequest.statusText);
}
// </snippetJQueryRESTDataOperations.Functions>
Comments
Post a Comment