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());
}