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

<UPDATE date=”2011-02-14″>: As of jQuery 1.5, single quotes are *required* around z:row or any other similar node selector. This was actually “required” in previous versions of jQuery, but not enforced.

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

</UPDATE>

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.

<UPDATE date=”2009-12-09″>: I’ve arrived at a better answer for the z:row (or any namespaced XML) selector issue based on something even simpler from  Kevin Whinnery.  A nice side benefit of this approach is that it seems to work with Safari on the Mac, though since I don’t have access to a Mac, I haven’t tested very much.

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

</UPDATE>

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.

Similar Posts

17 Comments

  1. Thank you. Your getZRows() func at the top of the article is the only thing I’ve gotten to work for me with Chrome. I tried the baked in SPFIlterNode in SPServices, along with other ‘this worked for me…’ bits here. So should somebody find themselves tormented by this, keep at it and try, cry, again.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.