Thursday, January 15, 2015

Reference javascript file from HTMLin CRM 2011

When you reference a web resource you should always use the appropriate mechanism to do so. Again, the SDK contains the details but in summary:

Referencing web resources from one of our designers is straight forward and the designer will take care of doing the right thing when referencing the web resource
Referencing web resources from CRM component that don’t have a designer (e.g. Ribbon, Sitemap) should always use the $webresource directive
Referencing web resources from other web resources (e.g. an html page referencing a js library) should always use RELATIVE paths.
All the above boils down to one important fact; depending on the deployment type (and other configuration such as domain name, caching, etc) that CRM is running the absolute URL of a web resource will be different.  Simple example, a web resource whose unique name is “myprefix_/mypage.html” will translate into the following URLs:

CRM Online: https://myorg.crm.dynamics.com/Webresources/myprefix_/mypage.html
CRM On-premises: http://myserver/myorg/Webresources/myprefix_/mypage.html
So now you see why is so important to reference web resources appropriately. Both the CRM designers and the $webresource directive will take care of constructing the correct URL (and will also provide buil-in caching capabilities). As for referencing one web resource from another as long as you use relative references you should be fine. Just remember that any directory below /Webresources is out of your control.  In the example if myprefix_/mypage.html wants to reference a js file under the custom scripts directory (e.g. the web resource unique name of the js file is ”myprefix_/scripts/myscript.js” the code would look like this

<html>
<head>
<script src=”scripts/myscript.js”
</head>

</html>


Notice how I referenced the script file using relative URLs, this would cause the browser to traverse forward and find the correct js file on the scripts directory (simulated using the name of the script file). The above will work for all CRM deployments without having to change a single line of code.

No comments:

Post a Comment