web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Hosk's Dynamic CRM Blog / CRM 2011/2013 – JavaScript ...

CRM 2011/2013 – JavaScript Programming Best Practices

Hosk Profile Picture Hosk

Microsoft has a very good page on Javascript programming in CRM - http://msdn.microsoft.com/en-us/library/hh771584.aspx

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.

So I thought sharing the best practices as recommended by Microsoft would be a good idea.

JavaScript Programming Best Practices

The following sections describe best practices when you use JavaScript with Microsoft Dynamics CRM.

Avoid Using Unsupported Methods

On the Internet, you can find many examples or suggestions that describe using unsupported methods. These may include leveraging undocumented internal function for page controls. These methods may work but because they are not supported you can’t expect that they will continue to work in future versions of Microsoft Dynamics CRM. This is especially true with Update Rollup 12 and the December 2012 Service Update because of significant changes to the internal application functions to support browsers other than Internet Explorer. For more information, see this blog post:Resolve Breaking Script Issues When Upgrading Microsoft Dynamics CRM.

Use the Custom Code Validation Tool to identify code that is using unsupported methods.

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).

Do Not Create Unnecessary Dependencies on JavaScript Libraries

JavaScript libraries like jQuery are typically not required when you write functions that will be used as form event handlers. The Xrm.Page object model provides functions to perform all supported interactions with Microsoft Dynamics CRM entity forms. Libraries, such as jQuery, often provide capabilities to perform XMLHttpRequests using functions like $.ajax. However, you can perform XMLHttpRequests, with little additional code, in all supported browsers using the native XMHHttpRequest object provided by the browser. For example, compare the sample_/Scripts/SDK.JQuery.jsand sample_/Scripts/SDK.REST.js libraries used in SDK samples. These libraries perform the same actions for all browsers, except that SDK.REST.js does not have a dependency on jQuery.

Recognize Limitations for Content Delivery Network (CDN) Libraries

Content delivery network (CDN) JavaScript libraries provide many advantages for public websites. Because these libraries are hosted on the Internet, you do not need to create web resources that contain the content of the libraries. For Microsoft Dynamics CRM you should consider the following issues before you use a CDN JavaScript library.

  • Users of the Microsoft Dynamics CRM for Microsoft Office Outlook with Offline Access client have the capability to work with no Internet connection while working offline. If you are depending on an Internet connection for your JavaScript libraries, your code will fail.
  • Some organizations will restrict Internet access for employees. Unless they configure the network to allow access to the CDN library sites, your code may fail for those organizations.

The alternative to using CDN libraries is to create a script (JavaScript) web resource with the contents of the library. Because web resources are organization-owned entities they will be synchronized when a Microsoft Dynamics CRM for Outlook with Offline Access user goes offline. Because these web resources now become part of the application they will not be blocked if an organization restricts access to the Internet.

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.

Do Not Access the DOM

JavaScript developers are used to interacting with Document Object Model (DOM) elements in code. You might use the window.getElementById method or the jQuery library. You are free to use these techniques in your HTML web resources, but they are not supported to access elements in Microsoft Dynamics CRM application pages or entity forms. Instead, access to entity form elements are exposed through the Xrm.Page object model. The Microsoft Dynamics CRM development team reserves the right to change how pages are composed, including the ID values for elements, so using the Xrm.Page object model protects your code from changes in how pages are implemented. For more information, see Use the Xrm.Page Object Model.

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.


Filed under: CRM 2011, CRM 2013, Javascript, JScript

This was originally posted here.

Comments

*This post is locked for comments