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 problem with the example code is shown below in bold:

 

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("<View><RowLimit>100</RowLimit></View>");
      this.collListItems = employeeList.getItems(camlQuery);

      clientContext.load(<strong>this.</strong>collListItems, 'Include(Id, DisplayName, HasUniqueRoleAssignments)');

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

});
Advertisements