Get all list items using ECMAscript SharePoint 2010
05/16/2011 • Admin
Category: SharePoint 2010
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();
while(ListEnumerator.moveNext())
{
var currentItem = ListEnumerator.get_current();
TextFiled += currentItem.get_item('Title') + '-' +currentItem.get_item('Department') + '\n';
}
alert(TextFiled);
}
function failed(sender, args) {
alert("failed. Message:" + args.get_message());
}
</script>
<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




Recent Forum Posts
Linq to Sharepointby RS on May 18, 2012
comments on the codeby spbsmile on May 18, 2012
connection(add) EditControlBlock and my programby spbsmile on May 18, 2012
Custom master pages used in multiple SharePoint 2010 sitesby ubersteve on May 18, 2012
upload file and click on button but cant upload file into list by using below codeby santhoshreddy on May 18, 2012