Troubleshoot GetListItems Tool

Mar 29, 2013 at 3:41 PM
I see a lot of discussions here related to methods for retrieving filtered, sorted recordsets from list objects and people often get that "Aha!" moment when they see the simple stuff they did wrong. I go through a lot of that myself. It's often difficult to troubleshoot those things within the bigger project you're working. So, I developed a simple web part page to develop and test my GetListItems calls with queries and such. It's a quick and easy way to develop a call to any list and view the results in raw form and also in structured form. I'll share it here if you're interested. It involves 3 Content Editor Web Parts for the interface/display and a block of code. ....Geoff

CEWP - User Interface
<table>
    <tr>
        <td colspan=3>Do not enclose arguments below in double quotes("").  When citing specific values within a string, use single quotes ('ID', <em>not</em> "ID").</td>
    </tr>
    <tr>
        <td colspan=2><h3 class="ms-standardheader">Web URL</h3> (default = current site)</br><input id="sps_WebURL" type="text" style="width:803px" /></td>
        <td><button type="button" onclick="GetAllListItems(0)">Get List Items</button></td>
    </tr>
    <tr>
        <td><h3 class="ms-standardheader">List Name<span class=ms-formvalidation> *</span></h3><input id="sps_ListName" type="text" style="width:390px" /></td>
        <td><h3 class="ms-standardheader">View Name</h3><input id="sps_ViewName" type="text" style="width:390px" /></td>
        <td><h3 class="ms-standardheader">Row Limit</h3><input id="sps_CAMLRowLimit" type="text" style="width:50px" /></td>
    </tr>
    <tr>
        <td colspan=3><h3 class="ms-standardheader">CAML View Fields</h3> (default = &lt;ViewFields Properties='True' /&gt;)</br><textarea id="sps_CAMLViewFields" rows=3 cols=150></textarea></td>
    </tr>
    <tr>
        <td colspan=3><h3 class="ms-standardheader">CAML Query</h3><textarea id="sps_CAMLQuery" rows=3 cols=150></textarea></td>
    </tr>
    <tr>
        <td colspan=3><h3 class="ms-standardheader">CAML Query Options</h3> (default = &lt;QueryOptions&gt;&lt;IncludeMandatoryColumns&gt;FALSE&lt;/IncludeMandatoryColumns&gt;&lt;/QueryOptions&gt;)</br><textarea id="sps_CAMLQueryOptions" rows=3 cols=150></textarea></td>
    </tr>
    <tr>
        <td colspan=3>
            <h3 class="ms-standardheader">Sample View Fields and CAML Query/Option Strings</h3><br>
            &lt;ViewFields&gt;&lt;FieldRef Name='Title' /&gt;&lt;FieldRef Name='Value' /&gt;&lt;/ViewFields&gt;<br>
            &lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='ID' /&gt;&lt;Value Type='Integer'&gt;3701&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&lt;/Query&gt;<br>
            &lt;Query&gt;&lt;OrderBy&gt;&lt;FieldRef Name='Title' /&gt;&lt;/OrderBy&gt;&lt;/Query&gt;<br>
            &lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='TaskOrderID' LookupId='true' /&gt;&lt;Value Type='Lookup'&gt;254&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&lt;/Query&gt;<br>
            &lt;QueryOptions&gt;&lt;IncludeMandatoryColumns&gt;FALSE&lt;/IncludeMandatoryColumns&gt;&lt;/QueryOptions&gt;<br>
            &lt;QueryOptions&gt;&lt;ViewAttributes Scope='RecursiveAll' /&gt;&lt;/QueryOptions&gt;<br>&nbsp;
        </td>
    </tr>
</table>
<hr>
CEWP - Raw XML Return
<textarea rows="50" cols="95" id="sps_MyXMLTextBox">sps_MyXMLTextBox</textarea>
CEWP - Structured XML Return
<div>
    <img id="sps_ListPrevious" onclick="ListNext('Back')" alt="Previous Page" src="/_layouts/images/plprev1.gif" border=0 style="visibility:hidden" />&nbsp;
    <img id="sps_ListNext" onclick="ListNext('Forward')" alt="Next Page" src="/_layouts/images/plnext1.gif" border=0 style="visibility:hidden" />
    <hr>
</div>
<div id="sps_SPDebug">sps_SPDebug</div>
Code to make it run
//******************************************************************************
//SPServices Get List Items
//pages/Development Pages/SPSvsGetListItems.aspx
//******************************************************************************

//Correct your references as required
<script type="text/javascript" src="../../JavaScript/jquery.min.js"></script>
<script type="text/javascript" src="../../JavaScript/jquery.SPServices.min.js"></script>
<script type="text/javascript">

    //Page variables
    var strNewPageOption;
    var strListItemCollectionPositionNext;
    var arrPageBackData = [];

$(document).ready(function(){
    //Enable cross-domain requests in environments that do not support cors yet but do allow cross-domain XHR requests 
    $.support.cors = true;
});


function GetAllListItems(strPageOptions) {
//Will read the arguments on the page to complete the SPServices GetListItems operation
    var strWebURL;
    var strViewName = "";
    var strCAMLViewFields;
    var strCAMLQueryOptions = "";
    
    if($("#sps_WebURL").val() == "") {
        strWebURL = $().SPServices.SPGetCurrentSite();
    }
    else {
        strWebURL = $("#sps_WebURL").val();
    }

    if($("#sps_ListName").val() == "") {
        alert("You must enter a list name as a minimum to proceed.");
        return;
    }

    if($("#sps_CAMLViewFields").val() == "") {
        strCAMLViewFields = "<ViewFields Properties='True' />";
    }
    else {
        strCAMLViewFields = $("#sps_CAMLViewFields").val();
    }

    if(strPageOptions == 0) {
        arrPageBackData.length = 0;
    }
    if(strPageOptions == 0 && $("#sps_CAMLQueryOptions").val() == "") {
        strCAMLQueryOptions = "<QueryOptions><IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns></QueryOptions>";
    }
    else if(strPageOptions != 0) {
        strCAMLQueryOptions = strPageOptions;
    }
    else {
        strCAMLQueryOptions = $("#sps_CAMLQueryOptions").val();
    }

    if($("#sps_ViewName").val() != "") {
        strViewName = $("#sps_ViewName").val();
        if(strViewName != "" && strViewName.slice(0,1) != "{") strViewName = spsViewGUID(strWebURL, $("#sps_ListName").val(), $("#sps_ViewName").val());
    }

    //Make the GetListItems call
    $().SPServices({
        operation: "GetListItems",
        async: false,
        webURL: strWebURL,
        listName: $("#sps_ListName").val(),
        viewName: strViewName,
        CAMLViewFields: strCAMLViewFields,
        CAMLQuery: $("#sps_CAMLQuery").val(),
        CAMLRowLimit: $("#sps_CAMLRowLimit").val(),
        CAMLQueryOptions: strCAMLQueryOptions,
        completefunc: function(xData, Status){
            //Output the raw XML return
            $("#sps_MyXMLTextBox").val(xData.responseText);
            if(arrPageBackData.length > 0) {
                $("#sps_ListPrevious").css("visibility", "");
            }
            else {
                $("#sps_ListPrevious").css("visibility", "hidden");
            }
            if($(xData.responseXML).SPFilterNode("rs:data").attr("ListItemCollectionPositionNext")) {
                strListItemCollectionPositionNext = $(xData.responseXML).SPFilterNode("rs:data").attr("ListItemCollectionPositionNext");
                $("#sps_ListNext").css("visibility", "");
            }
            else {
                $("#sps_ListNext").css("visibility", "hidden");
            }
            //Output the structured XML return
            var strRtn = $().SPServices.SPDebugXMLHttpResult({
                node: xData.responseXML
            });
            $("#sps_SPDebug").html(strRtn);
        }
    });
}


function spsViewGUID(strWebURL, strList, strView) {
//Returns the GUID of view 'strView' of list 'strList'
    var strGUID;
    $().SPServices({
        operation: "GetViewCollection",
        async: false,
        webURL: strWebURL,
        listName: strList, 
        completefunc: function (xData, Status){
            $(xData.responseXML).SPFilterNode("View").each(function(){
                if($(this).attr("DisplayName") == strView) strGUID = $(this).attr("Name");
            });
        } 
    }); 
    return strGUID;
}


function ListNext(strDirection) {
//If Row Limit is used, will load the next X rows of data forward or backward by using the paging information available from previously loaded pages
    if(strDirection == "Forward") {
        //Next Page
        strNewPageOption = strListItemCollectionPositionNext;
        arrPageBackData.push(strListItemCollectionPositionNext);
    }
    else {
        //Previous Page
        if(arrPageBackData.length > 0) {
            arrPageBackData.pop();
            if(arrPageBackData.length > 0) {
                strNewPageOption = arrPageBackData[arrPageBackData.length - 1];
            }
            else {
                strNewPageOption = 0;
            }
        }
        else {
            strNewPageOption = 0;
        }
    }
    if(strNewPageOption != 0) {
        strNewPageOption = strNewPageOption.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
        if($("#sps_CAMLQueryOptions").val() != "") {
            var strUserOptions = $("#sps_CAMLQueryOptions").val();
            strUserOptions = strUserOptions.slice(0, strUserOptions.indexOf("</QueryOptions>"));
            strNewPageOption = strUserOptions + "<Paging ListItemCollectionPositionNext='" + strNewPageOption + "' /></QueryOptions>";
        }
        else {
            strNewPageOption = "<QueryOptions><Paging ListItemCollectionPositionNext='" + strNewPageOption + "' /></QueryOptions>";
        }
    }
    GetAllListItems(strNewPageOption);
}
Mar 29, 2013 at 3:45 PM
Oh, I forgot to mention I'm running jQuery 1.7.2 and SPServices 0.7.1a.

Geoff
Coordinator
Mar 29, 2013 at 3:46 PM
This is cool, Geoff. I'll have to try it out. Want to turn it into an SPServices Story?

M.
Mar 29, 2013 at 4:06 PM
I'm quite OK with that. Of course, my instructions and code above were written from my perspective. You may want to see how it runs for you first and make any necessary tweaks to my instructions/code comments to include things I may have overlooked.
Coordinator
Apr 2, 2013 at 9:47 PM
It's a nice little tool. I got it set up but have been sick since. I hope to do some more with it.

Have you used the U2U CAML Builder or the CAML Designer for SharePoint 2013?

M.
Apr 5, 2013 at 6:18 PM
I have not used either of those tools. I'm currently on SP2007 so the latter may not be an option for me. It appears the U2U CAML Builder is an application that requires installation. I'm on a very restricted (*.mil) network. As such, installing 3rd-party software such as that is not authorized without going through many months of a request, certification & accreditation process....very intense. I have basic user rights on my local machine but I am a SharePoint Site Collection Administrator for my site collection. That's why I rely so heavily on JavaScript to customize my sites. It's all I can use. I can't even get SPDesigner installed! If it weren't for web services and the code module you built, my site collection would be nothing more than a document dumping ground.
Dec 11, 2013 at 3:12 PM
Super helpful, thanks a lot