Sue Hernandez's SharePoint Blog

SharePoint and Related Stuff

MOSS – Iterate through List using JQuery and Ajax

Here’s some script using  jquery to iterate through a list when you only know the name of the list and not the GUID.  That helps when you want to use the same script on multiple sites.  This can go in a content editor web part.  Replace the locations of the JQuery Library and your Site URL. 

<script type='text/javascript' src='https://YourSiteHere/YourLibraryHere/jquery-1.4.2.min.js'></script>
<div id="divReportOutput"></div>
<script language="javascript">
var s = "<table width='100%' cellpadding='5' cellspacing='0'>";
    var soapRequest = '<?xml version="1.0" encoding="utf-8"?>
                       <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/">
                       <soap:Body><GetListCollection
                       xmlns="http://schemas.microsoft.com/sharepoint/soap/"></GetListCollection>
                       </soap:Body></soap:Envelope>';
    $.ajax({
        url: "https://YourSiteHere/YourSubSite/_vti_bin/Lists.asmx",
        type: "POST",
        dataType: "xml",
        data: soapRequest,
        complete: processResult,
        contentType: "text/xml; charset=\"utf-8\""
    });
}
function processResult(xData, status)
{
    var reportListGUID = "";
    var xDoc = xData.responseXML;
    try
    {
        var ID1 = xDoc.selectSingleNode("//List/@ID[../@Title='" + NameOfYourDocumentLibrary + "']");
        reportListGUID = ID1.value;
    }
    catch(ex) { reportListGUID = ""; }
    if(reportListGUID != "")
    {
        // With Query
        //var newSoapEnvelope = '<?xml version="1.0" encoding="utf-8"?>
                                 <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/"><soap:Body>
                                 <GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
                                 <listName>' + reportListGUID + '</listName>
                                 <query><Query><Where><Contains><FieldRef Name="FileLeafRef" />
                                 <Value Type="Text">' + YourDocumentName + '</Value></Contains>
                                 </Where></Query></query></GetListItems></soap:Body></soap:Envelope>';
        // Everything
        var newSoapEnvelope = '<?xml version="1.0" encoding="utf-8"?>
                               <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/"><soap:Body>
                               <GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
                               <listName>' + reportListGUID + '</listName></GetListItems>
                               </soap:Body></soap:Envelope>';
        $.ajax({
            url: urlToQuery,
            type: "POST",
            dataType: "xml",
            data: newSoapEnvelope,
            complete: processListItemsResult,
            contentType: "text/xml; charset=\"utf-8\""
        });
    }
}
function processListItemsResult(xData, status)
{
    var xDoc = xData.responseXML;
    var matchingNodes = xDoc.selectNodes("//z:row");

    for(var i = 0; i < matchingNodes.length; i++)
    {
        var alternating = "";
        if(i % 2 == 1)
        {
            alternating = " class='ms-alternating'";
        }

        var uploadedOn = xDoc.selectSingleNode("@ows_Modified");
        var linkNode = xDoc.selectSingleNode("@ows_FileRef");
        var link = linkNode.value.substr(linkNode.value.indexOf("#") + 1);
        s += "<tr" + alternating + "><td class='ms-vb'><a href='" + link + "'>" + link + "</a></td>
              <td class='ms-vb'>Uploaded On: " + uploadedOn.value + "</td></tr>";
    }
    // Output the text
    s += "</table>";
    $('#divReportOutput').html(s);
}
</script>
Advertisements

5 responses to “MOSS – Iterate through List using JQuery and Ajax

  1. Pingback: Tweets that mention MOSS – Iterate through List using JQuery and Ajax « Sue Hernandez's Blog -- Topsy.com

  2. Matt B. July 15, 2010 at 12:09 am

    Whew, you have put in some work here… Have you looked at SPServices? Marc has oversimplified the process of doing Ajax calls via Web Services. You may want to check it out.

  3. anonymous April 26, 2011 at 5:08 pm

    so much complicated …

  4. Sandeep October 13, 2011 at 11:03 am

    Codeplex > SPServices project is the answer

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

%d bloggers like this: