If you are like me, you love JavaScript and use it frequently. One thing I’ve been missing is the ability to execute Fetch statements against the 2011 Organization service through JavaScript. With this basic FetchUtil JavaScript library, now you can use the 2011 service. After using this, hopefully you will find some nice little enhancements baked into the 2011 endpoint.
There are basically two files involved here. The core of it all is the FetchUtil.js which will actually build the SOAP XML and execute against CRM 2011. The second part is a small little HTML (with JavaScript embedded) showing you an example use of the FetchUtil.js file.
FetchUtil.js
var XMLHTTPSUCCESS = 200;
var XMLHTTPREADY = 4;
function FetchUtil(sOrg,sServer)
{
this.org = sOrg;
this.server = sServer;
if (sOrg == null) {
if (typeof(ORG_UNIQUE_NAME) != "undefined") {
this.org = ORG_UNIQUE_NAME;
}
}
FetchUtil.prototype.Fetch = function(sFetchXml, fCallback)
{
/// <summary>Execute a FetchXml request. (result is the response XML)</summary>
/// <param name="sFetchXml">fetchxml string</param>
/// <param name="fCallback" optional="true" type="function">(Optional) Async callback function if specified. If left null, function is synchronous </param>
var _oService;
var _sOrgName = "";
var _sServerUrl = "
function executeFetchCommand()
{
var sFetch = document.getElementById('txtFetch').value;
_oService = new FetchUtil(_sOrgName, _sServerUrl);
var oEntity = _oService.Fetch(sFetch, myCallBack);
}
function myCallBack(results){
var sOut = "";
sOut += "<b>XML Response</b><br />";
sOut += results;
document.getElementById('dvData').innerHTML = sOut;
}
Now that we have our JavaScript utility and our Fetch example, we need to do a few things.
We need a solution to put these two files into (feel free to add it to an existing solution or create one named whatever you like).
Let’s now import the FetchUtil.js file and publish. Take note of the Publisher Prefix (the letters in front of the name).
We now need to modify the fetchExample.htm file to point to this FetchUtil.js by replacing the {pub} with your Publisher Prefix (ex. “new_FetchUtil.js”).
We also need to modify the _sServerUrl and change the {org} to the appropriate organization abbreviation. If you are using the onsite, you will need to change the entire URL to match accordingly.
Finally, let’s upload the fetchExample.htm file and publish.
This should now bring you to a super fancy screen to paste some Fetch into.
Well there you have it. The next step is to parse the XML with jQuery, RegEx, traversing the nodes, or however you prefer. Hope you enjoy!
1 thought on “Execute Fetch from JavaScript in CRM 2011”
Mark
Hi, do I need to run this code from within CRM? From my understanding and from samples i've seen online, I can't call CRM webservices from outside of CRM. Does this sound correct?
Thanks
Comments are closed.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Hi, do I need to run this code from within CRM? From my understanding and from samples i've seen online, I can't call CRM webservices from outside of CRM. Does this sound correct?
Thanks