Paging with JSOM
By Anatoly Mironov
If there are many list items you try retrieve with javascript object model,paging could be very useful. Today I came across a wonderful blog post series about javascript object model in SharePoint: The SharePoint javascript object model - Resources and Real World Examples posted by David Mann and published on Aptilon Blog. There is an example how to achieve paging with JSOM. The key is items.get_listItemCollectionPosition() and query.set_listItemCollectionPosition() I have refactored David’s example to avoid global variables and to put into a module. Here is it. If you have a Tasks list in your site with many items, just hit F12 to open the console and paste this and see the result:
(function(SP) {
var
ctx = SP.ClientContext.get\_current(),
list = ctx.get\_web().get\_lists().getByTitle('Tasks'),
position,
enumerator,
view = '<View><ViewFields><FieldRef Name="Title"/></ViewFields><RowLimit>10</RowLimit></View>',
query = new SP.CamlQuery(),
items,
init = function() {
query.set\_viewXml(view);
},
loadChunks = function() {
**query.set\_listItemCollectionPosition(position)**;
items = list.getItems(query);
ctx.load(items);
ctx.executeQueryAsync(success, error);
},
success = function() {
console.log("\\nFound Matching Items! ");
enumerator = items.getEnumerator();
while(enumerator.moveNext()) {
console.log("Title: " + enumerator.get\_current().get\_item("Title") );
}
position = **items.get\_listItemCollectionPosition()**;
//when there are no items position is null
position && loadChunks();
},
error = function(sender, args) {
console.log('Request failed. Error: ' + args.get\_message() + '. StackTrace: ' + args.get\_stackTrace());
};
init();
loadChunks();
})(SP);
- async
- CAML
- Client Object Model
- console
- devtools
- javascript
- jsom
- module
- paging
- patterns
- sharepoint
- SharePoint 2010
- SPList
- SPListItem