CRM JavaScript Programming Best Practices in different versions

Microsoft has a very good page on Javascript programming in CRM –

I was reading this today and I noticed it had a Javascript Programming Best Practices section.  Javascript is very useful but I often feel the code produced in Javascript is not alway the best quality because there is no type checking and the process of writing the code and uploading is a bit of a hassle.


I have selected a couple of the best Practices but to read the full list go to the article

Microsoft have also created best practices for different versions, links to below to the different pages

CRM 2011 – Javascript best practices

CRM 2013 – Javascript best practices

CRM 2015 – Javascript best practices


There are a lot of best practices and it’s a really good article, but below are my top 4 best practices from the article.  I would definitely recommend you read the different articles.


Use a Cross-browser JavaScript Library for HTML Web Resource User Interfaces

A cross-browser JavaScript library, such as jQuery, provides many advantages when developing HTML web resources that must support multiple browsers. JavaScript libraries like jQuery provide a unified development experience for all browsers supported by Microsoft Dynamics CRM. These capabilities are most appropriate when you are using HTML web resources to provide user interfaces. JavaScript libraries like jQuery provide consistent ways to interact with the Document Object Model (DOM).

Use Feature Detection When Writing Functions for Multiple Browsers

Even when you use a cross-browser library like jQuery, you need to be very aware of differences between browsers. You can generally detect which browser is being used by querying thenavigator.useragent property. This is called browser detection. Browser detection is not a good strategy for most cases because it can’t take into account what features newer versions of a browser have. Also, some browsers provide the capability to modify the navigation.useragent property so that they appear to be a different browser.

Feature detection is the recommended approach. By detecting what features are available, you can create code paths for the browsers you support without knowing exactly which browser is being used. For more information about feature detection, see How to Detect Features Instead of Browsers.

Define Unique Names for Your JavaScript Functions

When you are the only developer for an HTML page you can easily manage the names of the JavaScript functions you use. In Microsoft Dynamics CRM, other solutions may add JavaScript functions to the page where your function is used.

If two JavaScript functions on a page have the same name, the first function defined is overwritten by the second. For this reason, make sure that you define unique names for your JavaScript functions. For more information, see Creating Script Libraries.

Use Asynchronous Data Access Methods

When you access data by using the Microsoft Dynamics CRM web services that use the REST or SOAP endpoint for web resources, always use an XMLHttpRequest that is configured to execute asynchronously. The reason is that the browser operates on a single thread. If that thread is being used to execute a long-running process synchronously the browser will stop responding.

4 thoughts on “CRM JavaScript Programming Best Practices in different versions

  1. ukcrmguru November 24, 2014 / 7:58 pm

    I agree that “sharing” this article is a good idea. Signposting it, pointing out that it exists, linking to it and recommending people to read it are all good.
    Ripping it off and simply copying and pasting other people’s content is not the way to do this. I am sure Microsoft like having people directed to their content. I am not sure they like having their intellectual property stolen quite so much.
    Every time you make a copy of an article you dilute the ability of people to find the original.

    You discovered this Microsoft article on MSDN, but for some reason you seem happy to then take it and use it as if it is your own, which makes it harder for others to find it the same way.

    With technical references like MSDN and TechNet this is particularly annoying – Microsoft spend lots of time, effort and money making sure pages like this one are kept up to date and accurate. Making a one-off copy at a point in time totally defeats this purpose. Your article is headed “CRM 2011/2013 JavaScript Programming Best Practices” – but in fact you only copied the 2011 article. You make no reference to the later articles for 2013 and 2015, which have some particularly interesting points to make about JQuery. But your readers may not realise there is more to be read – it looks like you have the same content here, so why would they bother to follow the link?

    Why not use your blog to add some value – add comments about which points in particular you have found useful in the past, add some ideas of your own about what else you would suggest people do to help them write better code.

    You write some excellent content when you try, but for every good post I see, I also come across something like this. You have been very quick to criticise others in the past when you feel “your” content is being copied, but for some reason you feel you do not need to give others the same courtesy that you expect to be extended to yourself:


    • Hosk November 24, 2014 / 9:36 pm

      Excellent comment as usual Adam and I agree with your comments and I have changed the article to link to the different versions of the document.

      I have removed the majority of the article and put my top 3 to give people an idea of the content.

      I agree it’s not good practice to create blog posts like this and I no longer do this and will tidy up my older blog posts as I see them. This article was written a while ago and my blog I now focus on creating content of my own rather than linking to other posts.

      I appreciate you taking the time to write such a constructive comment.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s