Get all list items using ECMAscript SharePoint 2010

Stay In the Loop

In this post you will learn about how to retrieve all the items from a list using ECMA\JavaScript client object model, and also how to enumerate through them. The code below will retrive items from all folders and sub-folders as well. To retrieve items from a specific folder see the post Here

For below script, I have used  a custom list ‘Testlist” which has columns (“Title and Department”) and has three list items.

You can copy and paste the below script in a Content editor webpart for testing purposes.

Please Note : You might need to fix the quotes ” & ‘ after you copy the below code.

<script src="/_layouts/SP.js" type="text/ecmascript"></script>
<script type="text/javascript">

function ViewItem()
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('Testlist');

var query = SP.CamlQuery.createAllItemsQuery();
allItems = list.getItems(query);
context.load(allItems, 'Include(Title,Department)');

context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));

function success() {

var TextFiled = "";
var ListEnumerator = this.allItems.getEnumerator();

var currentItem = ListEnumerator.get_current();
TextFiled  += currentItem.get_item('Title') + '-' +currentItem.get_item('Department') + '\n';

function failed(sender, args) {
alert("failed. Message:" + args.get_message());


<a href="#" onclick="Javascript:ViewItem();">View Items</a>

Result –

Also See:
Get item columns\fields using Client Object Model Ecmascript

Retrieve items from a specific Folder Ecmascript\Javascript client object model SharePoint 2010