Customization and programming tricks by Ronny Van der Snickt

Speed up development with external JavaScript files

When creating complicated JavaScript for MS CRM it can be easier to place your code in an external JavaScript file while developing. This content of this file will be loaded and executed in the onload event of the form.

To load this file place the following code in the onload event.

var url = “‘/ISV/” + ORG_UNIQUE_NAME + “/javascripts/” + crmForm.ObjectTypeName + “.js’”;
st = document.createElement(“<script src=” + url + ” language=’JavaScript’>”);
h = document.getElementsByTagName(“head”);
h[0].insertAdjacentElement(“beforeEnd”, st);

 For this to work your file need to be saved like this
<<MS CRM Website>>\ISV\<<Organization Name>>\javascripts\<<entity name>>.js

For example:
I want to create a javascript file to be loaded in the onload of an account form (for the organization advworks). On my development server I first create a file at the following location:
c:\Inetpub\Microsoft Dynamics CRM\ISV\advworks\javascripts\account.js

Next I cut and paste the above JavaScript code in the onload event of the account save and publish it.
Notice I don’t need to change or configure the javascript code in order to make this work! This also works for custom entities.

Al I have to do is start writing JavaScript code in de account.js file, save the file and open an account in MS CRM.

By doing this you can save a lot of time because you don’t need to save your script in the form properties and open the preview form or publish every time if you want to test with live data.

This method is not supported so make sure that after you created and tested your script you cut and paste your script from the file to the onload of the form and remove the JavaScript that loads up your external file.

Happy scripting

Comments

Comment from Dimaz Pramudya
Time: January 6, 2010, 5:21 am

Hi, do you know why this method is unsupported?
From what I can tell you are just creating the script tag on the fly and referencing the js file in ISV folder.

Comment from Ronny Van der Snickt
Time: January 18, 2010, 2:33 pm

There isn’t much harm in it, but thats what the SDK is telling us (at least in the MS CRM 3.0 SDK I don’t know if it’s still in the 4.0 SDK). You just have to know that the script is loaded after the onload. So if a user changes a value and there is an onchange event that uses a function declared in the external file, there is possibility the function is not loaded yet and you get an error.

Comment from mahipalreddy
Time: February 16, 2010, 12:13 pm

how can i pass parameters to js functions

Comment from fredsmith
Time: June 3, 2010, 9:57 am

Hi, there is an supported way of doing this using a tool called CRM Configuration Manager, You first export all JS to a folder in your VS Project, then create a bat file using the tool to import all your JS to any CRM server. And use the Bat file in Post Compile and in installation scripts.

You can find the tool here, http://blog.halan.se/page/CRM-Configuration-Manager.aspx

Comment from Alan
Time: September 17, 2010, 8:56 pm

Great solution, thanks! I maintain 12 identical sites on an enterprise server, and this will definately help with standardization.

I’m having a problem… seems like CRM is caching the contents of the entity.js file. I modified and saved the file, however, when I reopen the form, I get results as if the original .js file was there. Do I need to do an IIS reset or something else every time I modify the .js file? Thanks.

Comment from admin
Time: September 29, 2010, 9:40 am

In the same folder where the javascript files are saved create a new file web.config and put the following in the file:

After you clear your browser cache one last time the JavaScript files will no longer be cached.

Write a comment