Many times I’ve used the examples in this article to get lists items from SharePoint using JavaScript.

The examples in this article are however incorrect and the collection of list Items isn’t available within the onQuerySucceeded function.

The corrected example code is shown below:

[code lang=text]
function onQuerySucceeded(sender, args) {
var listItemInfo = ”;
var listItemEnumerator = collListItems.getEnumerator();

while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
listItemInfo += ‘\nID: ‘ + oListItem.get_id() +
‘\nTitle: ‘ + oListItem.get_item(‘Title’) +
‘\nBody: ‘ + oListItem.get_item(‘Body’);
}
alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {
alert(‘Request failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
}

$(document).ready( function() {
try {
var siteUrl = ‘/sites/pm’;

var clientContext = new SP.ClientContext(siteUrl);

var employeeList = clientContext.get_web().get_lists().getByTitle(“Employees”);

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(“100”);
this.collListItems = employeeList.getItems(camlQuery);

clientContext.load(this.collListItems, ‘Include(Id, DisplayName, HasUniqueRoleAssignments)’);

clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
} catch(err) {
alert(err);
}

});
[/code]

Avatar for Pieter Veenstra

By Pieter Veenstra

Business Applications Microsoft MVP working as a Principal Architect at HybrIT Services Ltd. You can contact me using contact@sharepains.com

2 thoughts on “SharePoint – Get list items using JavaScript”
    1. Hi Praveen,
      That is exactly what I tried as the MSDN article suggested the same however it didn’t work. The MSDN article doesn’t seem to be right. When I used the this keyword, then the variable wasn’t available within the succeeded function.

      As soon as I used a global variable it worked.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from SharePains by Microsoft MVP Pieter Veenstra

Subscribe now to keep reading and get access to the full archive.

Continue Reading

%d bloggers like this: