Quantcast
Channel: jQuery Library for SharePoint Web Services
Viewing all articles
Browse latest Browse all 6517

New Post: Troubleshoot GetListItems Tool

$
0
0
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);
}

Viewing all articles
Browse latest Browse all 6517

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>