08
Nov
09

SharePoint’s Web Services, jQuery, and the z:row Namespace in Safari and Chrome

jQuery’s totally cross-browser capable, right?  Well, generally, yes.  However, through no fault of jQuery, Safari and Chrome don’t seem to like the z:row namespace that the SharePoint Lists Web Service GetListItems operation returns.  I found a nice workaround for z:row from Mike Hacker to improve cross-browser compatibility.  I added a check for ItemCount up front so that we don’t try to use the getElementsByTagNameNS function in IE if there have been no rows returned legitimately.

Here’s my function.  I call it with the XMLHttpRequest result wherever I’m using GetListItems to get a usable rowset regardless of the browser.

// Find the rows in an XMLHttpRequest.  This includes a workaround for Safari and Chrome's
// aversion to the z:row namespace.
function getZRows(rXML) {
    var rows;
    var itemCount = $(rXML).find("rs\\:data").attr("ItemCount");
    if (rXML.getElementsByTagName("z:row").length == 0 && itemCount == undefined) {
        rows = rXML.getElementsByTagNameNS('*', 'row');
    } else {
        rows = rXML.getElementsByTagName("z:row");
    }
    return rows;
}

I’ve tested this in IE8, Firefox, Safari, and Chrome on my Windows PC.  It still doesn’t seem to solve the issue on Safari for Mac, but I’m getting there.  This will at least let the functions in v0.5.0 of my jQuery Library for SharePoint Web Services work with all of the main browsers on the Windows side of things.


4 Responses to “SharePoint’s Web Services, jQuery, and the z:row Namespace in Safari and Chrome”


  1. November 12, 2009 at 1:15 am

    Marc, you can simplify this expression:
    rows = rXML.getElementsByTagName(“z:row”)||rXML.getElementsByTagNameNS(“*”,”row”);
    This is what I use on the image rotator for example (see the “build your own” tab):
    http://www.pathtosharepoint.com/Pages/ImageRotator.aspx

    This is ok in plain JavaScript, but for jQuery there’s actually a better way:
    $(rXML).find(“rows”)
    That’s it!

    The downside is that jQuery doesn’t handle namespaces, so you may need to add a namespace test later in the script. However, it doesn’t seem to be a concern here, as you were using a wildcard.

    • November 12, 2009 at 8:49 am

      Thanks, Christophe. As with anything, this library is under continual improvement (I hope!). Suggestions like this are always helpful.

      M.

      • November 16, 2009 at 11:44 am

        The bad news: the “find” method I suggested above doesn’t seem to work so well (at least with multiple namespaces).
        The good news: the workaround below seems to address the issue:
        http://kevinwhinnery.com/post/165178165/jquery-xml-parsing-and-xml-namespaces

        If you test it, I’d be interested in your conclusions, as I plan to use it myself.

  2. November 16, 2009 at 11:30 pm

    Christophe:

    I like Kevin Whinnery’s approach much more than mine because it’s cleaner, smaller code. I just did a quick test, and this:

    $(xData.responseXML).find("[nodeName=z:row]")

    seems to work fine in IE, Firefox, Safari, and Chrome. I’ll want to do some more testing before I declare victory, but I like it, and thanks!

    M.



 

November 2009
M T W T F S S
« Oct    
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Twitter Updates