locked
Custom Code Validation, can't use ActiveXObject("Microsoft.XMLDOM") anymore?? RRS feed

  • Question

  • I just downloaded and ran the custom code validation tool to prep for the multiple browser support, and got a hit on some code that I am using to parse the results of a FetchXML query.

    FetchUtil.prototype._FetchCallback = function (xmlhttp, callback) {
        ///<summary>(private) Fetch message callback.</summary>
        //xmlhttp must be completed
        if (xmlhttp.readyState != XMLHTTPREADY) {
            return;
        }
    
        //check for server errors
        if (this._HandleErrors(xmlhttp)) {
            return;
        }
    
        var sFetchResult = xmlhttp.responseXML.selectSingleNode("//a:Entities").xml;
    
        var resultDoc = new ActiveXObject("Microsoft.XMLDOM");
        resultDoc.async = false;
        resultDoc.loadXML(sFetchResult);
    
        //parse result xml into array of jsDynamicEntity objects
        var results = new Array
    (resultDoc.firstChild.childNodes.length);
    ...
    }

    The two lines in question are these:

    -----

    var sFetchResult = xmlhttp.responseXML.selectSingleNode("//a:Entities").xml;

    var resultDoc = new ActiveXObject("Microsoft.XMLDOM");

    ----

    So an ActiveXObject is not supported by anything other than IE?  Are there cross-browser compatible alternatives to this to allow me to parse the XML that comes back, or do I need to scrap this and start all over again?



    • Edited by ChrisH5491 Monday, July 2, 2012 5:46 PM
    Monday, July 2, 2012 5:45 PM

Answers

  • I need to do some regression testing, but I replaced the code and it seems to be working now.  I originally got the code from this blog, and have posted to the author to confirm if he thinks this will be ok.

    http://www.crmsoftwareblog.com/2011/07/using-the-fetchxml-crm-2011-service-within-a-javascript-web-resource/comment-page-1/#comment-12570

    // ===================================================
     // Old Code that is IE Specific
     // ===================================================
     var sFetchResult = xmlhttp.responseXML.selectSingleNode(“//a:Entities”).xml;
     var resultDoc = new ActiveXObject(“Microsoft.XMLDOM”);
     resultDoc.async = false;
     resultDoc.loadXML(sFetchResult);
     
    //parse result xml into array of jsDynamicEntity objects
     var results = new Array(resultDoc.firstChild.childNodes.length);
     for (var i = 0; i < resultDoc.firstChild.childNodes.length; i++) {
     var oResultNode = resultDoc.firstChild.childNodes[i];
     …
     
    // ===================================================
     // New Code that is cross broswer compatible??
     // ===================================================
     var sFetchResult = xmlhttp.responseXML.selectSingleNode("//a:Entities");
     var resultDoc = sFetchResult.selectNodes("a:Entity");
     
    //parse result xml into array of jsDynamicEntity objects
     var results = new Array(resultDoc.length);
     for (var i = 0; i < resultDoc.length; i++) {
     var oResultNode = resultDoc[i];
     …
    

    • Marked as answer by ChrisH5491 Saturday, July 21, 2012 6:09 PM
    Tuesday, July 3, 2012 1:38 PM

All replies

  • No; ActiveXObject is not supported in browsers other than IE.

    You don't need to explicitly declare the XML DOM object at all.  You are extracting the XML content as a string and loading it into an object, but the responseXML is already an XML DOM Document object, so why not continue to use it?

    var entityContainer = xmlhttp.responseXML.selectSingleNode("//a:Entities");
    
    var entities = entityContainer.selectNodes("a:Entity");
    
    for (var i = 0; i < entities.length; i++) {
      // do stuff with each entity
      var entity = entities[i];
      ...
    }

    Don't worry about the warnings regarding XML DOM parsing such as selectSingleNode(..); as long as you are using it for parsing XML from SOAP queries or similar and not directly interrogating or manipulating the DOM of the Entity Form, it will remain supported - see the following for an explanation (False Positives):

    http://pogo69.wordpress.com/2012/07/02/crm-2011-custom-code-validation-tool-ur9-aka-r8-preparation/


    --pogo (pat) @ pogo69.wordpress.com

    Monday, July 2, 2012 8:04 PM
  • Ok, I'll give that a try.  Thanks!
    Monday, July 2, 2012 8:16 PM
  • I need to do some regression testing, but I replaced the code and it seems to be working now.  I originally got the code from this blog, and have posted to the author to confirm if he thinks this will be ok.

    http://www.crmsoftwareblog.com/2011/07/using-the-fetchxml-crm-2011-service-within-a-javascript-web-resource/comment-page-1/#comment-12570

    // ===================================================
     // Old Code that is IE Specific
     // ===================================================
     var sFetchResult = xmlhttp.responseXML.selectSingleNode(“//a:Entities”).xml;
     var resultDoc = new ActiveXObject(“Microsoft.XMLDOM”);
     resultDoc.async = false;
     resultDoc.loadXML(sFetchResult);
     
    //parse result xml into array of jsDynamicEntity objects
     var results = new Array(resultDoc.firstChild.childNodes.length);
     for (var i = 0; i < resultDoc.firstChild.childNodes.length; i++) {
     var oResultNode = resultDoc.firstChild.childNodes[i];
     …
     
    // ===================================================
     // New Code that is cross broswer compatible??
     // ===================================================
     var sFetchResult = xmlhttp.responseXML.selectSingleNode("//a:Entities");
     var resultDoc = sFetchResult.selectNodes("a:Entity");
     
    //parse result xml into array of jsDynamicEntity objects
     var results = new Array(resultDoc.length);
     for (var i = 0; i < resultDoc.length; i++) {
     var oResultNode = resultDoc[i];
     …
    

    • Marked as answer by ChrisH5491 Saturday, July 21, 2012 6:09 PM
    Tuesday, July 3, 2012 1:38 PM
  • Looks good to me - all standard Javascript.

    --pogo (pat) @ pogo69.wordpress.com

    Tuesday, July 3, 2012 11:53 PM
  • ChrisH5491,

    I realize this is an old thread, but did this change work for you?  I am facing the same issue and appreciate any help:

    // ===================================================
     // New Code that is cross broswer compatible??
     // ===================================================
     var sFetchResult = xmlhttp.responseXML.selectSingleNode("//a:Entities");
     var resultDoc = sFetchResult.selectNodes("a:Entity");
     
    //parse result xml into array of jsDynamicEntity objects
     var results = new Array(resultDoc.length);
     for (var i = 0; i < resultDoc.length; i++) {
     var oResultNode = resultDoc[i];

    Thanks!

    Thursday, November 13, 2014 12:51 AM
  • Yes, I can confirm that this did work and I have been using it for a couple of years now in CRM 2011.

    Thanks!

    Thursday, November 13, 2014 12:31 PM
  • Can you paste entire fetchxml code. I am getting multiple errors at various places with browser compatibility.

    My fetchxml is working fine in IE but getting error in chrome at few places.

     

    var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

    var sFetchResult = xmlhttp.responseXML.selectSingleNode("//a:Entities").xml;

    var resultDoc = new ActiveXObject("Microsoft.XMLDOM");

    Saturday, May 2, 2015 3:07 AM
  • In chrome I am getting error at the below line

    var sFetchResult = xmlhttp.responseXML.selectSingleNode("//a:Entities");

    Saturday, May 2, 2015 3:30 AM