Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

In sharepoint 2010 client object model in javascript, I want to create a caml query, that can download items from a list. I have this part working, but I want to download only x items, not the whole thing. Also I want to be able to get the first x items, but starting from position y.

Is this possible to do in CAML query?

EDIT:

// This part downloads the list information and adds it to the DOM
function getRelventItems(office, cat) {

    // reset ajax checking functions
    window.ajax_calls = 0;
    window.can_reveal_menu = false;

    display_loading(false);

    // remove the markerplace suffix to match the check box names in the edit form
    office = office.replace(" Marketplace","");

    // build the where clause
    var specifier = "";
    if (office == "" && cat == "") {
        specifier = "<Eq><FieldRef Name='" + window.fields[8] + "'/><Value Type='User'><UserID/></Value></Eq>";
    } else if (office == "All Offices" && cat != "All") {
        specifier = "<Eq><FieldRef Name='" + window.fields[3] + "'/><Value Type='MultiChoice'>" + cat + "</Value></Eq>";
    } else if (office != "All Offices" && cat == "All") {
        specifier = "<Eq><FieldRef Name='" + window.fields[2] + "'/><Value Type='MultiChoice'>" + office + "</Value></Eq>";
    } else if (office != "All Offices" && cat != "All") {
        specifier = "<And><Eq><FieldRef Name='" + window.fields[2] + "'/><Value Type='MultiChoice'>" + office + "</Value></Eq><Eq><FieldRef Name='" + window.fields[3] + "'/><Value Type='MultiChoice'>" + cat + "</Value></Eq></And>";
    }

    // create a caml query, and make sure the downloaded info is sorted in order of modified data in ascending order
    var camlquerystring = "<View><Query><Where>" + specifier + "</Where><OrderBy><FieldRef Name='Modified' Ascending='FALSE' /></OrderBy></Query><RowLimit>" + window.pagelimit.toString() + "</RowLimit></View>";   

    // get the list
    var list = window.web.get_lists().getByTitle(window.listname);

    // create the caml query
    var query = new SP.CamlQuery();
    query.set_viewXml(camlquerystring);


    var position = new SP.ListItemCollectionPosition();
    position.set_pagingInfo("Paged=TRUE&p_ID=1"); //to get items from the second
    query.set_listItemCollectionPosition(position); 

    // download items from sharepoint list using the caml query
    this.listItems = list.getItems(query);
    window.clientContext.load(listItems, 'Include(' + get_context_string() + ')');

    // set up an on-success function and an on-failure function
    window.clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess),Function.createDelegate(this, this.onQueryFailed));
}
share|improve this question

1 Answer 1

Check this post. The main point is to use "RowLimit" in your query:

query = new SP.CamlQuery(); 
query.set_viewXml("<View><RowLimit>10</RowLimit></View>"); 

And set listItemCollectionPosition property:

var position = new SP.ListItemCollectionPosition();
position.set_pagingInfo("Paged=TRUE&p_ID=1"); //to get items from the second
query.set_listItemCollectionPosition(position); 
share|improve this answer
    
for the position, is it 0 based index or 1 based index? like to start from the second item for example, do I give 1 or 2 as the position? –  omega May 30 '13 at 15:15
    
I tried var query = new SP.CamlQuery(); query.set_viewXml(camlquerystring); query.set_listItemCollectionPosition(0); but it didn't work, the query came back with a error. I was trying to get items starting form the first item. –  omega May 30 '13 at 15:21
    
ListItemCollectionPosition is not a number, it is an object with property PagingInfo that you need to update. I updated my answer with example. –  Andrew May 30 '13 at 15:53
    
It didn't work, I am still getting all the items. To give more information, I am getting the list items sorted by modified date desc. And I want the items starting from the second item (after its sorted). So the item ID doesn't count here I believe. –  omega May 30 '13 at 16:30
    
Could you provide the code that you used? –  Andrew May 30 '13 at 16:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.