Today we posted an update to the SharePoint AJAX Toolkit to support changes in IE10. The SharePoint AJAX Toolkit is a library designed to take an XML URL and an XSLT URL and spit the HTML out into a div. The heart of the control is the JavaScript XmlControl, which is wrapped by the AjaxXmlWebPart. At NewsGator we use the AjaxXmlWebPart for a MAJOR portion of our UI code, it’s just so handy to take an XML data source which we can update on the client for a client refesh, and point it at a simple XSLT transform. Add a little post-render jQuery code, and you’ve got REALLY sexy functionality in very little time. A similar JSON-based approach is in the works, but the XML technique is a little more powerful (and REALLY easy if you keep it simple). But I digress… this post is about changes to AJAX processing for IE 10.
Over the years, we’ve found that the Microsoft AJAX Library (AKA “Atlas” for you old timers!) was a bad idea. Not long after it shipped, Microsoft acknowledged this and said something like “yeah, that was a bad idea, just use jQuery.” Which of course made me feel REALLY great about my book that covered a lot of their framework… ouch! Still a good book, just skip those chapters on the client side AJAX library and read a good jQuery reference. You can find the book pretty cheap over on Amazon.
But again, I digress.
Here’s the change you should know:
When you need to get back an XmlDOM for doing XML things with (like XSLT) you need to TELL the request that the response type is an MSXML document (that sounds so wrong) otherwise it end up being a DOM document, which doesn’t have the XSLT functions you need. Simply set the responseType to msxml-document and it’s majically an XML object when it comes back.
Of course, the (not) awesome part about this is that this breaks all those silly JavaScript libraries (like Microsoft’s Sys.Net.WebRequest) and forces you to go old-school do-it-yourself for the request. Which isn’t all that hard, luckily.
Our old code looked like this:
var request = new Sys.Net.WebRequest();
request.get_headers()["X-SPAJAX"] = “XmlControl.LoadXml”;
request.set_url(url);
request.requestdate = new String(new Date());
request.get_headers()["X-REQUESTDATE"] = request.requestdate;
request.add_completed(this.xmlLoaderDelegate);
request.invoke();
Our new code now looks like this:
var request = SharePointAjax.GetXmlRequester();
request.open(“GET”, url, true);
if (request.setRequestHeader)
request.setRequestHeader(“X-SPAJAX”, “XmlControl.LoadXml”);
var requestDate = new String(new Date());
request.setRequestHeader(“X-REQUESTDATE”, requestDate);
var xc = this;
request.onreadystatechange = function () {
if (request.readyState === 4) {
xc.LoadXmlComplete(request, requestDate);
}
};
try { request.responseType = ‘msxml-document’; } catch (e) { };
request.send();
…where the GetXmlRequester is a simple function that looks like this, and provides compatibility for those dinosaur versions of Internet Explorer out there. It’s almost not needed, but for your mom who’s stuck on Windows Me, it might just help out.
SharePointAjax.GetXmlRequester = function(){
if (window.XMLHttpRequest)
return
new XMLHttpRequest();
var progIDs = ['Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP'];
for (var i = 0, l = progIDs.length; i < l; i++) {
try {
var xmlAx = new ActiveXObject(progIDs[i]);
return xmlAx;
}catch (ex) {}
}
}
That’s about it! Thanks to SharePointJohn (part of our incredible NewsGator dev team) and the good folks at the IEBlog for assistance.
Until next time… happy Labor Day to everyone!