Wednesday, February 13, 2013

JavaScript, XML processing, DOM and CRM 2011 UR12

If you have scripts in CRM which somehow retrieve or process XML, chances are you will need to do some re-write or validations when applying Update Rollup 12 (UR12). This post explains how to fix you JavaScripts when you make use of the DOM parser for processing XML.

One of the highest-risk customizations to break with UR12 are JavaScripts and these are the 2 most common reasons why:

1. It is common for developers to perform unsupported operations with the page DOM instead of using the supported xRM APIs. Since UR12 introduces cross-browser support, accessing the DOM by unsupported methods such as ‘getElementById’ on CRM forms will no longer work because it has changed. Therefore, customizations like this one will break.

2. Because CRM has historically only been supported in IE, developers might have written JavaScripts that depend on IE extensions specific to Microsoft, these will also stop working because the assumption is no longer true (will be broken even in IE!). Even though you are not doing anything unsupported in your scripts, they can break and might need to be fixed.

There are plenty of tools and blog posts that talk about how to tackle #1, so in this article I will focus on problem #2 above and I will pick the specific example of processing XML.

Assume you are parsing and processing an XmlDocument in your JavaScript, perhaps you retrieved the xDoc from and XML HTTP request, using AJAX or even if you are simply parsing text into an XmlDocument object. In my case, I was retrieving an XML web resource using the following code:

  1. function ShowHelpText() {
  2.     var tooltipsTextFile = "ava_TooltipsText.xml";
  3.  
  4.     // Use AJAX to retrieve the tooltips texts
  5.     $.ajax({
  6.         type: "GET",
  7.         async: true,
  8.         url: tooltipsTextFile,
  9.         dataType: "xml",
  10.         success: parseTooltipsXml,
  11.         error: function (xhr, textStatus, errorThrown) {
  12.             alert("Error retrieving tooltips: " + xhr.message);
  13.         }
  14.     }); //end ajax
  15. }

Before UR12, this code used to return a documentElement of type IXMLDOMElement which is a Microsoft extension to the W3C DOM. Therefore, I was happily calling methods such as selectNode and selectSingleNode. The problem is, after UR12, the same code will return a standard Document object which does not support selectSingleNode so I get the error “Object doesn’t support property or method ‘selectSingleNode’”:

image

 

In part, this is great news, because now your JavaScript will work in any browser (mostly), but you still need to do some clean-up to remove any dependencies on IE DOM extensions. In my case, I had to update my script to make use of getElementsByTagName and verify that I only use methods and properties that are W3C standard as documented here and process my XML according to that documentation (which works slightly different than if you have IE-specific JS).

Although my example is specific to parsing XML, the same can apply to any other scripts that rely on IE extensions not available in other browsers or not standard. I hope this post helps others be more aware and proactive before applying UR12 and in understanding how your scripts can break even if you are not using any unsupported customizations!

3 comments:

  1. Hi,

    I found this post googling an error I got on some custom script I pulled together for our Dynamics CRM 2011 deployment.

    You explain the problem exactly, but I am not a programmer and cobbled this script together from bits and pieces on the 'net. How would I fix this to be compliant with CRM post UR12?

    var sFetchResult = xmlhttp.responseXML.selectSingleNode("//a:Entities").xml;
    var resultDoc = new ActiveXObject("Microsoft.XMLDOM");
    resultDoc.async = false;
    resultDoc.loadXML(sFetchResult);

    ReplyDelete
  2. You will need use W3C standard that work in all browsers and not use any browser specific code such as "selectSingleNode" or ActiveXObject.

    ReplyDelete
  3. Great summary of the problem. Thanks for nothing.

    ReplyDelete