Not sure what I am doing wrong?

May 20, 2014 at 3:15 PM
The below code returns on record as it should......
    $(document).ready(function () {


                    $().SPServices({
                        operation: "GetListItems",
                        listName: "User Information List",
                        CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>5</Value></Eq></Where></Query>",
                        completefunc: function (xData, Status) {
                            $(xData.responseXML).SPFilterNode("z:row").each(function () {

metricsHtml = "<table><tr><td>" + $(this).attr("ows_Title") + "</td></tr>";
$("#myDiv").append(metricsHtml);


                           }); // end each
                     } // end completefunc
              }); // end spservices
  }); // end ready function
But as soon as I add the code below I get multiple records for one list.

```
$(document).ready(function () {

    $().SPServices({
        operation: "GetListCollection",
        async: false,
        completefunc: function (xData, Status) {
            $(xData.responseXML).find('List').each(function () {
                listName = $(this).attr("Title");
                listType = $(this).attr("ServerTemplate");
                listCreated = $(this).attr("Created");
                listModified = $(this).attr("Modified");
                listItemCount = $(this).attr("ItemCount");
                listView = $(this).attr("DefaultViewUrl");
                userId = $(this).attr("Author");

                $().SPServices({
                    operation: "GetListItems",
                    listName: "User Information List",
                    CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>5</Value></Eq></Where></Query>",
                    completefunc: function (xData, Status) {
                        $(xData.responseXML).SPFilterNode("z:row").each(function () {
metricsHtml = "<table><tr><td>" + listName + "</td><td>" + $(this).attr("ows_Title") + "</td><td>" + userId + "</tr></td>";
$("#myDiv").append(metricsHtml);
                        }); // end each
                    } // end completefunc
                }); // end spservices
              }); // end each
           } // end completefunc
        }); // end spservices
}); // end ready function
`````` Does anyone see what might be wrong?
Coordinator
May 20, 2014 at 3:30 PM
Well, you're calling GetListItems once per list in the list collection, so you should be seeing one user per list. I'm not sure why you've got the GetListItems cal embedded in the GetListCollection call since you're always getting user 5.

M.
May 20, 2014 at 3:45 PM
Edited May 20, 2014 at 5:04 PM
I had the 5 in just for testing purposes and forgot to put the userId back in.....
        $().SPServices({
            operation: "GetListCollection",
            async: false,
            completefunc: function (xData, Status) {
                $(xData.responseXML).find('List').each(function () {
                    listName = $(this).attr("Title");
                    listType = $(this).attr("ServerTemplate");
                    listCreated = $(this).attr("Created");
                    listModified = $(this).attr("Modified");
                    listItemCount = $(this).attr("ItemCount");
                    listView = $(this).attr("DefaultViewUrl");
                    userId = $(this).attr("Author");

                    $().SPServices({
                        operation: "GetListItems",
                        listName: "User Information List",
                        CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + userId + "</Value></Eq></Where></Query>",
                        completefunc: function (xData, Status) {
                            $(xData.responseXML).SPFilterNode("z:row").each(function () {
                                metricsHtml = "<tr><td>" + $(this).attr("ows_Title") + "</td><td>" + listName + "</td><td>" + listType + "</td><td>" + listItemCount + "</td><td>" + listCreated + "</td><td>" + listModified + "</td><td>" + listView + "</tr></td>";
                                $("#myDiv table").append(metricsHtml);


                            }); // end each
                        } // end completefunc
                    }); // end spservices
                }); // end each
            } // end completefunc
        }); // end spservices
    }); // end ready function
May 20, 2014 at 5:34 PM
The below code works but the table cell doesn't show up for createdBy. However with an alert it does have the correct vales and if I move any code around I get unexpected results. Any ideas?
 $(document).ready(function () {

var metricsHtml = "<table id='metrics'><tr><td width='150'>Site Name</td><td width='150'>List Name</td><td width='150'>List Type</td><td width='150'>Item Count</td><td width='250'>Created Date</td><td width='250'>Modified Date</td><td>Default List View</td></tr>";

        $("#myDiv").append(metricsHtml);

        $().SPServices({
            operation: "GetListCollection",
            async: false,
            completefunc: function (xData, Status) {
                $(xData.responseXML).find('List').each(function () {
                    listName = $(this).attr("Title");
                    listType = $(this).attr("ServerTemplate");
                    listCreated = $(this).attr("Created");
                    listModified = $(this).attr("Modified");
                    listItemCount = $(this).attr("ItemCount");
                    listView = $(this).attr("DefaultViewUrl");
                    userId = $(this).attr("Author");

                    metricsHtml = "<tr><td>Test</td><td>" + listName + "</td><td>" + listType + "</td><td>" + listItemCount + "</td><td>" + listCreated + "</td><td>" + listModified + "</td><td>" + listView + "</td>";

                    $().SPServices({
                        operation: "GetListItems",
                        listName: "User Information List",
                        CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + userId + "</Value></Eq></Where></Query>",
                        completefunc: function (xData, Status) {
                            $(xData.responseXML).SPFilterNode("z:row").each(function () {
                                createdBy = $(this).attr("ows_Title");
  
                                metricsHtml = "<td>" + createdBy + "</td></tr>";

                            }); // end each
                        } // end completefunc
                    }); // end spservices

                    $("#myDiv table").append(metricsHtml);

                }); // end each
            } // end completefunc
        }); // end spservices
    }); // end ready function

</script>
<div id="myDiv"/>
May 21, 2014 at 5:28 PM
The issue was with async not being set to false on the second SPServices call. Apparently async means something...........who would have thought :)
Coordinator
May 21, 2014 at 5:30 PM
Oops. Sorry I didn't spot that for you. Glad you figured it out!

You should start learning about using promises with your calls rather than using synchronous calls, but that may be for another day.

M.