Learning SharePoint   Enhance Your SharePoint Knowledge

Subscribe to Learning SharePoint Subscribe
Twitter Learning SharePointFollow Us
in Learning SharePoint
Check Out Our New SharePoint 2013 series!

Get all list items using ECMAscript SharePoint 2010

05/16/2011  
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

Subscribe to Learning SharePoint Subscribe
Twitter Learning SharePointFollow Us
in Learning SharePoint
Advertisement

Random Posts

PropertyOrFieldNotInitializedException in Client Object model Sharepoint 2010
PropertyOrFieldNotInitializedException in Client Object»

You will definitely come across PropertyOrFieldNotInitializedException exception at some point during your client object model coding. In this post I»

Powershell Interview questions SharePoint 2010
Powershell Interview questions SharePoint»

Q. What is Windows Powershell ? Ans. Windows PowerShell is a new Windows command-line shell designed especially for system administrators.»

Taking the BCS list offline in Sharepoint 2010
Taking the BCS list»

After you’ve created and secured your external list, SharePoint supports taking the list offline so that you can program against»

Recent Forum Posts

Linq to Sharepoint

by RS on May 18, 2012

comments on the code

by spbsmile on May 18, 2012

connection(add) EditControlBlock and my program

by spbsmile on May 18, 2012

Custom master pages used in multiple SharePoint 2010 sites

by ubersteve on May 18, 2012

upload file and click on button but cant upload file into list by using below code

by santhoshreddy on May 18, 2012