SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

List of Lists using CEWP, WS, JS

When I first started playing with Portal Server a couple of years ago, my experience was only with WSS.  One thing that bugged me about Portal was the fact that there was no "easy" way to display links of all the lists in an area to the users without creating another list just for links.  This is because the standard user permissions we had set up didn't allow them to see the "Manage Content" link.

An easy way I've found to create links to an area is by using HTML/JavaScript in a CEWP and querying the SharePoint Web Service.

Paste the following code into a Content Editor Web Part (in SharePoint Portal 2003) and it will show you all of the lists in the area (the DWP is also attached to this post).

<span id=ListList>&nbsp;</span>

<script language=javascript>

//Lists in area
//Uses SharePoint Web Service API to retrive list of lists in current area
//Author: Aaron Haydo, ashaydo@gmail.com
//Date creaed: 6-JUL-2007

getListList();

function getListList() {

  var txt = document.getElementById("ListList");
  
  //Build SharePoint Web Service URL based on current location
  var wsURL;
  wsURL = window.location.protocol+"//";
  wsURL += window.location.host;
  var path = window.location.pathname.split("/");
  path.pop();
  var x;
  for (x in path) {
    wsURL += path[x] + "/";
  }
  wsURL += "_vti_bin/lists.asmx";
  
  //SOAP Action and XML
  var wsSoapAction = "http://schemas.microsoft.com/sharepoint/soap/GetListCollection";
  var wsXML = '<?xml version="1.0" encoding="utf-8"?>';
  wsXML += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ';
  wsXML += 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ';
  wsXML += 'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
  wsXML += '<soap:Body>';
  wsXML += '<GetListCollection xmlns="http://schemas.microsoft.com/sharepoint/soap/" />';
  wsXML += '</soap:Body>';
  wsXML += '</soap:Envelope>';

  //Create XML Document and get HTTP response using XMLHTTP object
  var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  var httpResponse = getServiceResults(wsURL, wsSoapAction, wsXML);
  
  //If getServiceResults returns a 404, then it's probably because the 
  //page is being launched from a document library instead of a web part
  if (parseInt(httpResponse) == 404) {
    txt.innerHTML = "<p>This code can only be executed from a web part.</p>";
    return;
  }
  else {
    xmlDoc.loadXML(httpResponse);
  }
  
  //Get results into collection
  listitems = xmlDoc.getElementsByTagName("List");

  //Loop through results and build table rows
  var output = "";
  for (var x = 0; x < listitems.length; x++) {
    output += "<tr title='";
    output += listitems(x).getAttributeNode("Description").text.replace(/'/g,"`");
    output += "'>";
    output += "<td class='ms-vb-icon' valign='top'>";
    output += "<a href='" + listitems(x).getAttributeNode("DefaultViewUrl").text + "'>";
    output += "<img src='";
    output += listitems(x).getAttributeNode("ImageUrl").text;
    output += "' border=0 hspace=5 alt='Icon'>";
    output += "</a>";
    output += "</td>";
    output += "<td class='ms-vb2' valign='top'>";
    output += "<a href='" + listitems(x).getAttributeNode("DefaultViewUrl").text + "'>";
    output += listitems(x).getAttributeNode("Title").text;
    output += "</a>";
    output += " (" + listitems(x).getAttributeNode("ItemCount").text + ")";
    output += "</td>";
    output += "</tr>";
  }
  
  //Display table
  var table = "";
  table = "<table border='0' width='100%' cellpadding='2' ";
  table += "cellspacing='0' class='ms-summarystandardbody' rules='rows'>" ;
  table += output;
  table += "</table>";
  txt.innerHTML = table;
}

function getServiceResults(url, soap, xml) { 
  //Send XML packet to web service and return HTTP response text
  try {
    if (xml.length > 0) {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      xmlHttp.open("POST", url, false); 
      xmlHttp.setRequestHeader("SOAPAction", soap); 
      xmlHttp.setRequestHeader("Content-Type", "text/xml"); 
      xmlHttp.send(xml);
      if (parseInt(xmlHttp.status) == 404) {
        return 404;
      }
      else {
        return xmlHttp.responseText;
      }
    }
  }
  catch(e) {
    alert(e.message);
  }
}
</script>

Here is what the Web Part looks like:

List of Lists


Posted 07-16-2007 8:29 AM by AaronH

Comments

Daryl wrote re: List of Lists using CEWP, WS, JS
on 09-05-2008 11:16 AM

Very cool, thanks for the example

sharavathi wrote re: List of Lists using CEWP, WS, JS
on 09-18-2008 6:58 AM

hi,

nice post.

can you suggest me, how to loop throght list items.

how to get the list column items.

thanks,

sharavathi

bahareh wrote re: List of Lists using CEWP, WS, JS
on 10-29-2008 8:12 AM

help me plz

I want to add item with javascript and lists.asmx into sharePoint list. but I can,t successful. Can u help me?

Hari wrote re: List of Lists using CEWP, WS, JS
on 01-15-2009 10:00 AM

Excellent Example.  Its just like listviewer webpart which we need to deploy.  

Bob Lalonde wrote re: List of Lists using CEWP, WS, JS
on 05-19-2009 10:51 AM

If I don't want the icons, what do I suppress in the script?

AaronH wrote re: List of Lists using CEWP, WS, JS
on 05-19-2009 1:11 PM

Take out the following lines to prevent the images from being displayed:

   output += "<a href='" + listitems(x).getAttributeNode("DefaultViewUrl").text + "'>";

   output += "<img src='";

   output += listitems(x).getAttributeNode("ImageUrl").text;

   output += "' border=0 hspace=5 alt='Icon'>";

   output += "</a>";

mark wrote re: List of Lists using CEWP, WS, JS
on 06-22-2009 3:42 PM

Wow.  

I am wanting to show the Announcements from the top level team site down on the teams sites within.

what do I need to edit to do this?

Great post....thank you

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.