Using the Content Editor Web Part to Query list from other sites through SharePoint Web Services

Posted: November 6, 2009 in MOSS 2007

Using the Content Editor Web Part to Query list from other sites through SharePoint Web Services

 

                So I recently ran into a situation where I had a client who had a hosted solution that did not allow for the Content Query Web part or any custom web part or component coding. All we had was the content editor web part and some creative JavaScript coding to help us.

                The goal was to display data from a list located on the root site on each of the 35 subsites in the farm without custom coding, without having to recreate and maintain the same list or make any linked lists, lookup fields, etc on all of the subsites.  I found a number of SPS03 sites that got me nearly where I needed to be. I took their code and converted it to a format that matched what MOSS returned from the web service call.

The solution was to insert the code below into the source editor of a content editor web part:

<span id=’uniquecontrolName’></ span>

<script language=javascript>

getListList();

 

function getListList() {

  var txt = document.getElementById(‘ uniquecontrolName ‘);

 

  //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 = "http://something.something.com/_vti_bin/lists.asmx&quot;;

 

//new soap action and xml

 var wsSoapAction = "http://schemas.microsoft.com/sharepoint/soap/GetListItems&quot;;

 var wsXML = ‘<?xml version="1.0" encoding="utf-8"?>’;

 wsXML += ‘<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd="http://www.w3.org/2001/XMLSchema&quot; xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">&#8217;;

 wsXML += ‘<soap:Body><GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">&#8217;;

 wsXML += ‘<listName>List name or List GUID</listName>’;

 wsXML += ‘<query></query>’;

 wsXML += ‘<queryOptions><QueryOptions><IncludeMandatoryColumns>TRUE </IncludeMandatoryColumns><DateInUtc>TRUE</DateInUtc><viewFields><ViewFields><FieldRef Name="Event%20Date" /></ViewFields></viewFields></QueryOptions></queryOptions>’;

 wsXML += ‘</GetListItems></soap:Body></soap:Envelope>’;

 

//Create XML Document and get HTTP response using XMLHTTP object

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

try

{

var httpResponse = getServiceResults(wsURL, wsSoapAction, wsXML);

if (parseInt(httpResponse) == 404) {

    txt.innerHTML = "<p>This code can only be executed from a web part.</p>";

    return;

  }

  else {

    xmlDoc.loadXML(httpResponse);

  }

 

}

  catch(e) {

    alert(e.message);

  }

 

//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("z:row");

 

  //Loop through results and build table rows

  var output = "";

  for (var x = 0; x < listitems.length; x++) {

 

    output += "<tr>";

    output += "<td>" + listitems(x).getAttributeNode(‘ows_Event_x0020_Date0’).text + "</td>";

    output += "<td><a href=’http://privateplacement.edensandavant.com/Lists/Calendar/DispForm.aspx?ID=&quot; + listitems(x).getAttributeNode(‘ows_ID’).text + "’>" + listitems(x).getAttributeNode(‘ows_Title’).text + "</a></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>

Advertisements
Comments
  1. Rick King says:

    Great article!! Thanks for posting this!

    I’m having a problem specifying a query. I’m having a problem even doing something simple like this:

    wsXML += ”;

    Am i missing something? If i leave it “</query", i get everything in the list but as soon as i add a simple 'order by' or 'where' element, nothing gets displayed.

    Thanks for your post, and help. 🙂
    Rick

  2. Rick King says:

    I found out the answer to my own question. the wsXML += “”; needs to be , uppercase “Q”. 🙂

    Thanks for the great post!
    Rick

    • mstarr13 says:

      Cool. Thanks for posting. Yeah the CAML queries are fun little creatures. GREAT stuff when they work, but frustrating as heck when they are off by even the slightest bit.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s