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

This is my code:

When I call the function to get the items, it fails (doesn't go to the fail function, it goes into the success function but crashes when I try to get the position object), and I get the error:

[13:17:57.273] Error: The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

If I don't use the context string, then it works ok. But I need to get the base permissions, as well as only get some of the fields.

Does anyone know how to fix this?

Thanks.

// occurs when the document is ready
$(document).ready(function() {
    // wait for the sharepoint javascript libraries to load, then call the function 'Initialize'
    ExecuteOrDelayUntilScriptLoaded(runCode, "sp.js");
});


var listItems;                   // The list of retrieved items.
var query;                       // For paging, reuse the same query object.
var targetList;                  // The list from which to retrieve items.
var clientContext;

function runCode() {
    clientContext = new SP.ClientContext();
    web = clientContext.get_web();  
    targetList = web.get_lists().getByTitle("Marketplace");
    query = new SP.CamlQuery();

    var CAML = "<View><Query><OrderBy><FieldRef Name='Modified' Ascending='FALSE' /></OrderBy></Query><RowLimit>5</RowLimit></View>";
    var CS = "Include(EffectiveBasePermissions,Title,Description,Office,Category,Contact_x0020_Number,Second_x0020_Contact_x0020_Numbe,Contact_x0020_Email,Web_x0020_Site,Author,Editor,Created,Modified,ID,Attachments)";

    //Specifying the RowLimit will determine how many items will be fetched in one call to the server.
    query.set_viewXml(CAML);
    listItems = targetList.getItems(query);
    clientContext.load(listItems,CS);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) {
    var message = "Titles, two at a time:\n";
    var listEnumerator = listItems.getEnumerator();
    while (listEnumerator.moveNext()) {
        message += "\nTitle=" + listEnumerator.get_current().get_item("Title")
    }
    alert(message);

    //Gets the id of the last element from the returned collection along with the query.
    var position = listItems.get_listItemCollectionPosition();
    //Position will be null if all the items in the collection are fetched and there are no more items to be fetched.
    if (position != null) {
        //If more items are to be fetched, make a second call to the server and fetch the next group of items.
        query.set_listItemCollectionPosition(position);
        listItems = targetList.getItems(query);
        clientContext.load(listItems);
        //Call the same function recursively until all the items in the current criteria are fetched.
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),Function.createDelegate(this, this.onQueryFailed));
    } 
}

function onQueryFailed(sender, args) {
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
share|improve this question
    
I have almost exact same problem with you. I am using sharepoint 2013 hosted app and with query = SP.CamlQuery.createAllItemsQuery(); it works fine but with query = new SP.CamlQuery(); var qstr = '<View><Query></Query></View>'; query.set_viewXml(qstr); it raise exception in success delegate on var itemCollection = items.getEnumerator(); "The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. " let me know if your issue has been solved.... –  Tomohisa Takaoka Sep 27 '13 at 9:52

2 Answers 2

The thing is when you call clientContext.load(listItems,CS) you retrieve just particular set of columns - this doesn't include ListItemCollectionPosition. If you check the response in any http debugger you'll see that. If you need paging information and limit columns to just what you need I suggest you to use ViewFields sub-element of View element and not use Include statement.

share|improve this answer
    
I am trying to understand what ListItemCollectionPosition does. I want to sort the list by modified date descending, but as well do paging. But from what I understand about paging, is it will give you the id of the last item returned, so the next time you get items, it will give you all items that's ID is greater than what you got before. Since I am sorting by modified date, doesn't this mess that up? –  omega Jun 1 '13 at 2:31
    
That's correct - SharePoint paging will return you the ID of the last item in sorted (by modified date in your example) set. –  Vadim Jun 4 '13 at 20:28
    
So then its not possible to do paging combined with sorting it in the caml query, right? –  omega Jun 4 '13 at 23:46
    
It is possible, everything will work fine. When you sort items by any field paged items will be sorted as well. You just need to use something like Paged=TRUE&p_SortBehavior=Modified&p_ID=7 in ListItemCollectionPosition. –  Vadim Jun 6 '13 at 1:20
    
Is there any documentation on how to use &p_SortBehavior=Modified, I am not able to find this anywhere. Also If I just try the paged=true and p_ID=_, then I get all ID's that's greater than _. It doesn't start at that index. –  omega Jun 6 '13 at 13:36

Before you call clientContext.load(listItems,CS), you should call query.set_listItemCollectionPosition(position). In first call you should assign null for position variable. Read more about working with paging: http://social.technet.microsoft.com/wiki/contents/articles/18606.paging-with-sharepoint-client-object-model.aspx

share|improve this answer

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.