Michael's profileMike's RavingsBlogLists Tools Help

Blog


    November 06

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

    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";

     

    //new soap action and xml

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

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

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

     wsXML += '<soap:Body><GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">';

     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=" + 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>