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]


Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Avatar of Pieter Veenstra

Is your business still running on paper trails, sprawling Excel files, or ageing Access databases? There's a better way — and I can show you exactly what it looks like. I'm the Technical Director of Vantage 365, a Microsoft solutions consultancy working with clients across the UK, the Netherlands, and worldwide. For over 30 years I've been turning messy, manual business processes into clean, automated systems that save time, reduce errors, and give teams the visibility they need to make better decisions. You can contact me using contact@sharepains.com

Related Posts

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

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

Continue reading