Cannot get list items from a Sharepoint list

Sep 13, 2010 at 11:11 AM

I am trying to get the list items with the following code, and although no errors are reported in the browser, nothing is displayed.

The full list url is the following;
http://mcanet/Units/InformationManagement/seanspiteri/Lists/Test1/AllItems.aspx

- all code put into a content editor webpart.
- jquery and spservices are stored on Sharepoint for easier access.
- the only alert that IS working is the first one in the .ready function.
- the alert in the completefunc is NOT firing.


<script src="/Lists/Site%20Code/Attachments/20/jquery-1.4.2.min.js" type="text/javascript"></script> <script src="/Lists/Site%20Code/Attachments/27/jquery.SPServices-0.5.6.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { alert("This alert is firing!"); $().SPServices({ operation: "GetListItems",   async: false,
  webURL: "/Units/InformationManagement/seanspiteri",
listName: "Test1", CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>", completefunc: function (xData, Status) { $(xData.responseXML).find("z:row").each(function() {
      alert("This alert is NOT firing!");
      $("#output").append($(this).attr("ows_Title") + "<br />"); }); } }); }); </script> <div id="output"></div>


Would appreciate some helpful hints on what might be the problem.

Thanks in advance,

Kevin

Coordinator
Sep 13, 2010 at 11:22 AM

Kevin:

Add an alert at the top of the completefunc like this:

alert(xData.responseXML.xml);

This will show you the XML result set and will probably help you to debug. The alert you have in the completefunc is within the .each(), so if there are no rows returned (for whatever reason) it won't fire.

Also look at this blog [post of mine for info on the z:row namespace with different browsers:
http://sympmarc.com/2009/11/08/sharepoints-web-services-jquery-and-the-zrow-namespace-in-safari-and-chrome/

M.

Sep 13, 2010 at 12:50 PM

Thanks for your quick reply and the debugging hints.

The alert displayed the List XML including the existing 3 items, so the problem is finding the z:row and displaying them.

I have added some more debugging code to the completefunc to try and locate the problem. All are commented, to know what worked and what didn't.


completefunc: function (xData, Status) {

// *** This is working - displaying the XML with 3 items returned.
alert(xData.responseXML.xml);

// *** This is working - appending test to the output div
$("#output").append("test");
// *** This is NOT working - nothing happens - tried to get the xml data to copy and paste the result here.
$("#output").append(xData.responseXML.xml);
$(xData.responseXML).find("z:row").each(function() { 

// The following 2 lines of code are NOT working either.
     alert($(this).attr("ows_Title"));
     $("#output").append($(this).attr("ows_Title"));

 $("#output").append($(this).attr("ows_Title") + "<br />"); }); } });

Thanks,

Kevin

Sep 13, 2010 at 1:58 PM

I found the culprit! :)

This line was not correct;

$(xData.responseXML).find("z:row").each(function() {

It has to have the double backslashes '\\' like so;

$(xData.responseXML).find("z\\:row").each(function() {


Still I do not know what is the meaning of those backslashes, why are they required? What is their function?


Kevin
Coordinator
Sep 13, 2010 at 3:25 PM

The first backslash escapes the second, which escapes the colon. ;+)  Read the blog post I posted above for a more reliable method.

M.

Sep 13, 2010 at 4:37 PM

Thought it had something to do with escaping...  but wasn't 100% sure :)

Thanks a lot for your help, probably couldn't have done it otherwise.

Kevin

Coordinator
Sep 28, 2010 at 11:22 AM

Kevin:

I saw in your other post that you still had questions about how best to get z:row. The post I gave a link to above shows the best method, which is:

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

This is the most reliable, as it works across every browser I've tested.

M.

Sep 28, 2010 at 11:30 AM

I see... oops... my mistake then... was trying all sorts of combinations except just simply and plainly replacing it as mentioned in the articles linked.

Sometimes things are so obvious!!! LOL :D - must be the stress! hehehe

Thanks,

Kev