Wednesday, July 24, 2013

How to load a web resource using Java Script

In Dynamics CRM 2011, if we implement JavaScript for particular form we add script web resource in the Form Libraries list under form customization, if there is any related script which need to be referred,  then we need to include that script as well in the Form Libraries. As you can see in the below screen, we have included “new_SDK.Rest” and “new_Json2”. We have not called the function of those scripts in the “Event Handlers”, but we have used them in our library “new_Account_Library.jsa


Now suppose if you need to add a button on the Home Page ribbon of entity, and this button should call some function in the “new_Account_Library.js” library. This means the button is *not* calling any HTML or Silverlight page.

In such case, if we use the HTML page instead of the javascript then you can load the related script library with the following line of code.

<script src="../" type="text/javascript"></script>


Here the problem is how to add/ load related JScript reference like in this case jscript and json Jscript reference. To retrieve the data from CRM we generally use functions from the library. Hence this library “” needs to be loaded as we are referring the function of “” library in this “new_Account_Library.js” script.

So to resolve this we can simply load referred web resource from JScript programmatically before performing our business logic, below is the function you can refer to.

We need to pass name of web resource.

LoadWebResource(Name of the Web Resource);




Function for load web resource:

//Function to Load webResource

function LoadWebResource(resource) {

 var httpRequest = null;

 try {    

if (window.XMLHttpRequest) {  // code for IE7+, Firefox, Chrome, Opera, Safari

            httpRequest = new XMLHttpRequest();

      else// code for IE6, IE5

httpRequest = new ActiveXObject("Microsoft.XMLHTTP");

              var serverUrl= Xrm.Page.context.getServerUrl();

             if (serverUrl.match(/\/$/)) {

serverUrl = serverUrl.substring(0, serverUrl.length - 1);

}"GET", serverUrl + "/webresources/" + resource, false);




catch (e) {
alert("LoadWebResource >> Error loading " + resource + ":\n" + e.description);



No comments:

Post a Comment