First Time Help Using GetListCollection

Jun 21, 2011 at 4:36 PM

Hello, 

This is my first time posting and I need help how to use the GetListCollection operation. I'm trying to collect the names of all the lists in a site and display it in a <div>, but it's not working for me. 

I'm not a programmer, so any help that is friendly for designer-type thinkers would be helpful! 

Here's where I am. I'm typing: 

$().SPServices({

operation: 'GetListCollection',

webURL: 'http://randomsite'

});

But I don't know what to do next! 

Thanks 

Jun 21, 2011 at 5:42 PM

This is something I put together a long time ago, hopefully it'll help you along:

		$().SPServices({					
			operation: "GetListCollection",
			completefunc: function (xData, Status) {
				$("#appendXML").val(xData.responseXML.xml);
				var liHTML = "";
				$(xData.responseXML).find("[nodeName=List]").each(function() {
					var img_URL = $(this).attr("ImageUrl");
					var list_ Title = $(this).attr("Title");
					var list_URL = $(this).attr("DefaultViewUrl");
					var list_Desc = $(this).attr("Description");
					var list_Created = $(this).attr("Created");
					var list_Modified = $(this).attr("Modified");
					var list_ItemCount = $(this).attr("ItemCount");
					
					var liHtml += "<ul><img alt=" + list_Title + " src=" + img_URL + " />   <b><a href=" + 
					list_URL + ">" + list_Title + "</a><br /><li>Description: </b>" + list_Desc +
					"<li><b>List Created: </b>" + list_Created + "</li><li><b>List Modified: </b>" + list_Modified +
					"</li><li><b>Item Count: </b>" + list_ItemCount + "</li></ul>";
				});
				$("#appendTo").append(liHtml).show(2000);
			}
		});

What you'll want to do is learn how to parse XML.  Using the documentation on this site is a great start to learn how to do that in the context of SharePoint.  My code above is just an example though, I'm sure you'll need to tweak it to suit your needs.

Cheers,

Matt

Jun 21, 2011 at 7:10 PM

That worked for me! 

Thanks Matt

Jun 22, 2011 at 4:29 PM

For some unknown reason, the first List entry apppears left-aligned with no image. The rest are indented with images. This is not related to the particular List that appears at the top, as it is happening across multiple sites/farms. Does anyone else see this or have any idea why this would happen? I don't see anything in the code that could make this happen.

Thanks

Steve

Coordinator
Jun 22, 2011 at 5:07 PM
Edited Jun 25, 2011 at 8:30 PM

Steve:

What Matt showed above is just an example of what you might do. His CSS may well be different than yours, and the CSS will determine the formatting.

M.

Jun 22, 2011 at 7:59 PM
Edited Jun 22, 2011 at 8:00 PM
EagleUK wrote:

For some unknown reason, the first List entry apppears left-aligned with no image. The rest are indented with images. This is not related to the particular List that appears at the top, as it is happening across multiple sites/farms. Does anyone else see this or have any idea why this would happen? I don't see anything in the code that could make this happen.

Thanks

Steve

I remember why now... If you look at the results coming back from the server, the img_URL is borked.  I remember updating that, so this must be an older version of my script.  You'll have to tweak it to your needs as well as the CSS as Marc said.

Jun 22, 2011 at 9:07 PM

Thanks, Matt.

I was not familiar with the term “borked” in a technical sense, but Urban Dictionary describes it very well. ;-)

As I mentioned, I didn’t see anything in your code example that would cause this, but am a bit rusty with javascript so thought that I would ask. Is there a best practice for examining the raw XML being returned?

Thanks again

Steve

From: iOnline247 [email removed]
Sent: Wednesday, June 22, 2011 8:00 PM
To: steve.eagleson@sky.com
Subject: Re: First Time Help Using GetListCollection [SPServices:262193]

From: iOnline247

EagleUK wrote:

For some unknown reason, the first List entry apppears left-aligned with no image. The rest are indented with images. This is not related to the particular List that appears at the top, as it is happening across multiple sites/farms. Does anyone else see this or have any idea why this would happen? I don't see anything in the code that could make this happen.

Thanks

Steve

I remember why now... If you look at the results coming back from the server, the img_URL is borked. I remember updating that, so this must be an older version. You'll have to tweak it to your needs as well as the CSS as Marc said.

Jun 22, 2011 at 10:15 PM

The script above actually has something like that already in it:

$("#appendXML").val(xData.responseXML.xml);

That line of code is definitely showing it's age b/c we are all using now:

alert(xData.responseText);

Definitely look over the previous discussions here.  There are some hidden nuggets of knowledge that are very useful and once you start learning more of them, your applications will look fantastic.

Jun 23, 2011 at 10:47 PM

The “alert(xData.responseText);” definitely works better – thanks very much. I can see that the SPServices call is working, and that the HTML string is being built up properly by using “alert(liHTML);”. But I am still having a problem when appending this to a DIV. For some reason, the first element is dropping the initial

    and tags. They appear as expected on the remaining elements, and everything works fine in Opera, just not in IE.

    I’ve upgraded to the latest jQuery and SPServices, but no help. I’ve also searched this list as well as the jQuery site to see if anyone else has had this problem, with no luck. I’ve also tried both the .append and .appendTo variants, but nothing seems to make a difference. This is extremely frustrating!

    If anyone has any ideas on how to fix this, I would love to hear them.

    From: iOnline247 [email removed]
    Sent: Wednesday, June 22, 2011 10:16 PM
    To: steve.eagleson@sky.com
    Subject: Re: First Time Help Using GetListCollection [SPServices:262193]

    From: iOnline247

    The script above actually has something like that already in it:

    $("#appendXML").val(xData.responseXML.xml);

    That line of code is definitely showing it's age b/c we are all using now:

    alert(xData.responseText);

    Definitely look over the previous discussions here. There are some hidden nuggets of knowledge that are very useful and once you start learning more of them, your applications will look fantastic.

Coordinator
Jun 25, 2011 at 7:43 PM
Edited Jun 25, 2011 at 8:29 PM

Matt's code above wasn't emitting valid HTML (LI tags weren't closed, etc.) I fiddled with it a bit and this works fine:

  var liHTML = "";
  $().SPServices({     
   operation: "GetListCollection",
   completefunc: function (xData, Status) {
    $("#appendXML").val(xData.responseXML.xml);
    $(xData.responseXML).find("[nodeName=List]").each(function() {
     var img_URL = $(this).attr("ImageUrl");
     var list_Title = $(this).attr("Title");
     var list_URL = $(this).attr("DefaultViewUrl");
     var list_Desc = $(this).attr("Description");
     var list_Created = $(this).attr("Created");
     var list_Modified = $(this).attr("Modified");
     var list_ItemCount = $(this).attr("ItemCount");
     
     liHTML += "<ul>" +
      "<li><img alt=" + list_Title + " src=" + img_URL + " /></li>" +
      "<li><b><a href=" + list_URL + ">" + list_Title + "</a></b></li>" +
      "<li><b>Description: </b>" + list_Desc + "</li>" + 
      "<li><b>List Created: </b>" + list_Created + "</li>" +
      "<li><b>List Modified: </b>" + list_Modified + "</li>" +
      "<li><b>Item Count: </b>" + list_ItemCount + "</li>" +
      "</ul>";
    });
    $("#appendTo").append(liHTML).show(2000);
   }
  });

M.