how to sharepoint 2010 client object model javascript list item ListEnumerator count
Result -
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.
my demo source.
return SharePoint List item Count !
function ViewItem() {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('test'); //list name
var query = SP.CamlQuery.createAllItemsQuery();
allItems = list.getItems(query);
context.load(allItems);
context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}
function success() {
var count = allItems.get_count();
alert(count);
}
function failed(sender, args) {
alert("failed. Message:" + args.get_message());
}
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 -
댓글 없음:
댓글 쓰기