Function

$().SPServices

Web Service

Lists

Operation

GetListItems

Notes

  • This operation accepts a webURL option. This allows you to change the context for the operation to a different site. Using a GUID for the listName does NOT change the context as it does with the Object Model, so you need to use the webURL option as well if your list is in a different site.

Tips

See Steve Ottenad's post about getting the MetaInfo in a useful way. The trick is to add

CAMLViewFields: "<ViewFields Properties='True' />",

to your call.

fereko noted that there is a QueryOption which isn't detailed in the SDK:

CAMLQueryOptions: "<QueryOptions><ExpandUserField>True</ExpandUserField></QueryOptions>"

returns account name, email, and name instead of just the name.

Example

This is an example from the SharePoint and jQuery  forum at Stump the Panel over at http://www.endusersharepoint.com.

In this example, we're grabbing all of the items in the Announcements list and displaying the Titles in a bulleted list in the tasksUL div.

If you have a better, real life example, please post it in the Discussions.

<script type="text/javascript" src="filelink/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="filelink/jquery.SPServices-0.6.2.min.js"></script>
<script language="javascript" type="text/javascript">

$(document).ready(function() {
  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Announcements",
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
        var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
        $("#tasksUL").append(liHtml);
      });
    }
  });
});
</script>
<ul id="tasksUL"/>

Last edited Mar 15, 2012 at 4:21 AM by sympmarc, version 8

Comments

DavidLewis27 Apr 4 at 1:28 PM 
Is it possible to get this to work for a custom column? I have a column that is a hyerlinked picture. I would like to use this code so that I can display the column a certain way. I am having trouble getting info back for this.

DavidLewis27 Apr 4 at 12:53 PM 
I have a custom column that is a picture with a hyperlink. Can I use this to return this type of field?

matturn Jul 4, 2012 at 12:22 AM 
http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems%28v=office.14%29.aspx is the relevent MS documentation for this function.

lazodev Apr 17, 2012 at 3:41 PM 
I was not sure where to share this but I have figured out a way to make you GetListItems dynamic to where it will get everything from your list every time even if you add new columns to the list, or remove columns from the list. This should save some time in coding. Tell me what you think:

//create new array for the list itmes
var listItems = new Array();

var j = 0;

//use SPServices GetList Method to get the names of your columns
$().SPServices({
operation: "GetList",
async: false,
listName: "Your List",
webURL: "Your URL",

completefunc: function(xdata, status)
{
$(xdata.responseText).find("Field").each(function() {
//alert(xdata.responseXML.xml);

var xitem = $(this);
var ListItem = xitem.attr('Name');
//put your list items into the FieldRef structure and load your array
listItems[j] = "<FieldRef Name='" + ListItem + "' />";

j++;

});
}
});

//remove the commas in your array with a little regex
var str = listItems.toString();
str = str.replace(/,/g, "");

//alert(str);
$().SPServices({
operation: "GetListItems",
async: false,
listName: "Your List",
webURL: "Your URL",
//call you array of list items into the <ViewFields> Tags
CAMLViewFields: "<ViewFields>"+str+"</ViewFields>",

completefunc: function(xdata, status)
{

alert(xdata.responseXML.xml);
$(xdata.responseXML).SPFilterNode("z:row").each(function() {

});
}
});

I hope this is useful. I thought it would make a nice function to call and reuse in your code. :)

Royaline Oct 25, 2011 at 5:08 PM 
CAMLQuery and CAMLQueryOptions need to be structured as (for example):

var myQueryOptions = "<QueryOptions><ViewAttributes Scope='RecursiveAll' IncludeRootFolder='True' /></QueryOptions>";
var myQuery = "<Query><Where><Eq><FieldRef Name='News_x0020_Section' /><Value Type='Choice'>Headline</Value></Eq></Where></Query>";

$(document).ready(
function () {
$().SPServices({
operation: "GetListItems",
async: false,
listName: "My List Name",
CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
CAMLQuery: myQuery,
CAMLRowLimit: 100,
CAMLQueryOptions: myQueryOptions,
completefunc: function (xData, Status) {
$(xData.responseXML).find("[nodeName='z:row']").each(function () {
// do work
});
}
});

kevincarbonaro Sep 13, 2010 at 2:02 PM 
Somehow I had problems getting the code to work in getting listitems. Just in case someone has a similar problem, this thread of mine may help :)
http://spservices.codeplex.com/Thread/View.aspx?ThreadId=227007&ANCHOR#Post492709