Safely add script references to dynamics crm pages

  • Digg
  • Facebook
  • LinkedIn
  • Twitter
 

Dynamics CRM is great, one of the great things is the ability to write custom javascript. Out of the box, unfortunately doesn’t give you the ability to reference external scripts which is a bit of a pain because you end up copy pasting/duplicating code all over the place.

Recommended methods

1. Dynamically load the script in the ‘OnLoad’ event, but if you have a common library which is used among multiple entities you end up duplicating the loading code, and since IIS caches the javascript if you update your common library you’ll need to instruct the users to clear their cache for the changes to take effect.

2. Another common suggestion is to edit the actual crm aspx page and inject your scripts into it, we strongly disagree with this since

  • It is an unsupported customization
  • You will be making the changes in multiple places anyway, eg: /SFA/accts/, /SFA/conts/ …etc
  • Rollups might override your file therefore keeping track of what files you changes becomes essential

Our method

We’ve created a HttpModule that hooks into the asp.net request life cycle, it monitors all asp.net requests, checks to see if the module is enabled in web.config, then checks to see if the current request is a page you want to hook into, then injects the specified scripts to the current request.

Configure the HttpModule

  1. Open web.config (The root web.config from the crm installation)
  2. Go into the HttpModules section and add the following line
    <add name="XrmLinqScriptInjector" type="XrmLinq.CrmTweaks.ScriptInjectionFilterModule" />
  3. Then locate the appSettings section and add the following lines
    <add key="xrmlinq.injectscripts" value="false" />
    <add key="xrmlinq.injectscriptsto" value="edit.aspx" />
    <add key="xrmlinq.scriptstoinject" value="/isv/xrmlinq/xrmlinq-1.0.0.js" />

    injectscripts – Enables/Disables the injection module
    injectscriptsto – Comma seperated list of all the pages you want the module to inject your scripts into
    scriptstoinject – Comma seperated list of all the scripts you want injected to the pages specified above

The result

  • Easy to include your custom global script libraries (eg: jQuery)
  • Upgrades are simple, simply change the setting to ‘/path/lib.js?version=xyz‘ and the user will get the new file immediately
  • Easy to maintain

Download the library by clicking on the link below.
http://www.xrmlinq.com/_builds/XrmLinq.CrmTweaks.zip

No Feedback

No feedback yet.

RSS feed for feedback on this post. TrackBack URI

Leave your feedback

© XrmLinq 2009.