SharePoint – Get list items using JavaScript

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:

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');

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();
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) {


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.


Please leave a comment or feedback

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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