Usually when you are working with CRM and doing scripting you have to work in the small text box with no real coding support. Also you have the restrictions that you cannot create reusable functions etc. There allready are several examples and approaches to solve this problem but for completion I have added these examples. This contains two different approaches one using files location on the CRM server the other using resources files.
The simple option to have you scripts located outside the CRM customizations is to use the following script:
var httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
httpRequest.open('GET', '/ISV/MyScript/ScriptName.js'), false);
httpRequest.send(null);
eval(httpRequest.responseText);With this approach you simple place the javascript files on the CRM webservice, typically inside a subfolder of the ISV. The script above downloads this script and includes it in the page. When using this approach you have to define your functions as: window.function = myFuncton(parameter) { ... }; otherwise they will not be accessible.
This approach allows you to define your scripts externally and use your own javascript editor.
This option is more complex as it requires visual studio but it does allow reusable scripts and localization of the javascripts. The basic method to load the script is the same as the example above but the script on the server side is nolonger just a file but it is loaded using an asp.NET handler. This handler retrieves the script from a resource file and processes it. During this processing other scripts can be included (for a example a library to perform soap calls from the script) and localization of strings can be done.
A sample solution is available here. This solution contains the handler and the script template to load the script.
window.loadScript = function(type) {
var httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
httpRequest.open('GET', prependOrgName('/ISV/ScriptHandler.ashx?type=' + type + '&orglcid=' + ORG_LANGUAGE_CODE + '&userlcid=' + USER_LANGUAGE_CODE), false);
httpRequest.send(null);
eval(httpRequest.responseText);
if (typeof (window.scriptLoaded) == 'undefined') {
alert('Form scripts could not be loaded. Contact your system administrator.');
window.scriptLoaded = null;
}
}
window.loadScript('[ScriptName]');After this all you need to do is publish you customizations.
If you have a library for example Daniel Cai's Javascript Web Service Toolkit to perform soap calls from script. You probaly want to reuse this. One approach would be to call the loadScript function for every script you want to include, but this would cause an additional http request with its associated overhead. The script handler supports a method to include the script on the server. This is done be inserting a tag /*Script:[ScriptName]*/, when the script is requested this will be replaced by the Script identified by [ScriptName].
If a include script is not found an error message (alert) is inserted instead.
Note that you should only include that what you need, including to much will slow the client down.
CRM Form scripts are hard to localize, the options you have are to build an if-statement everytime you want a localized string to be displayed or to load an external 'resource' file (a js file with string constants). The script handler provides an option to actually use the .NET resources for this. Like the script includes you use the tag /*Resources:[ResourceKey]*/ to include a resources. At run this will be replaced with the correct value from the resource file. Keep in mind that this is not context sensitive and strings are not encoded. So if you have a string "Show this 'example' here" this should be in the resource file as "Show this \'example\' here" and to show this as an alert the tag should be alert('/*Resources:example*/');
The localization will use the language desired by the user if it is specified even if no resources are available for this language. If the user language is not specified it will fallback to the CRM organization language. When neither is specified it will use the default resource file.