1

I am attempting to retrieve all items in a list via Javascript and log them to the console. Here is my code:

var showItems = function(title) {
    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web();
    var list = web.get_lists().getByTitle(title);
    var query = SP.CamlQuery();

    this.listItems = list.getItems(query);
    clientContext.load(listItems);
    clientContext.executeQueryAsync( Function.createDelegate(this, this.showListItemsSucceed), Function.createDelegate(this, this.showListItemsFail) );
};

var showListItemsSucceed = function(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = listItems.getEnumerator();

    while (listItemEnumerator.movenext()) {
        var item = listItemEnumerator.get_current();
        listItemInfo = '\nID: ' + item.get_id() + '\nTitle: ' + item.get_item('Title') + '\nBody: ' + item.get_item('Body');
    }

    console.log(listItemInfo.toString());   
};

var showListItemsFail = function(sender, args) {
    console.log("Show list items request failed. " + args.get_message() + '\n' + args.get_stackTrace());
};

This is pretty much straight from Microsoft Documentation at https://msdn.microsoft.com/en-us/library/office/hh185007(v=office.14).aspx where the docs say use a blank caml query to retrieve every item from the list. Except when I actually run the code (from chrome developer console) I get a "Show list items request failed. Unknown Error". If I instead pass a list title that isn't actually stored on the site, then I get a "Show list items request failed. List 'sadifoasidfj' does not exist at site with URL 'https://osisoft1980.sharepoint.com/teams/OBDev'.". So I know for sure that the list exists on the site and that the code is somehow recognizing that. Why isn't my code correctly getting the list items and logging them?

Thanks in advance.

3

Changed var query = SP.CamlQuery() to var query = new SP.CamlQuery() problem solved. Whoops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.