I have been having a problem with Javascript and Sharepoint 2013. I have a number of custom list rows that contain date and time values. I want to determine if there are any overlapping date spans. For this, I have written the following code:

$(document).ready(function () {
ExecuteOrDelayUntilScriptLoaded(highLightRedundantTimes, "sp.js");

var activitiesList;
var siteUrl;

var date;
var startTime;
var endTime;
var clientContext;
var camlQuery;
var listItems;
var enumerator;
var currentListItem;
var itemid;

var sublistItems;
var subenumerator;
var currentSubListItem;
var subdate;
var substartTime;
var subendTime;
var subcamlQuery;
var subitemid;

var ids = [];

function highLightRedundantTimes() {

    siteUrl = _spPageContextInfo.webAbsoluteUrl;

    clientContext = new SP.ClientContext(siteUrl);

    activitiesList = clientContext.get_web().get_lists().getByTitle('Activities');

    camlQuery = new SP.CamlQuery();


    listItems = activitiesList.getItems(camlQuery);


    clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);

function onQuerySucceeded(sender, args) {
    enumerator = listItems.getEnumerator();

    while (enumerator.moveNext()) {
        currentListItem = enumerator.get_current();

        startTime = currentListItem.get_item('ActivityStartTime');
        endTime = currentListItem.get_item('ActivityEndTime');
        date = new Date(currentListItem.get_item('ActivityDate'));
        itemid = currentListItem.get_item('ID');

        subcamlQuery = new SP.CamlQuery();
        subcamlQuery.set_viewXml('<View>' +
                                    '<Query>' +
                                        '<Where>' +
                                            '<And>' +
                                                '<Eq>' +
                                                    '<FieldRef Name=\'ActivityDate\' />' +
                                                    '<Value IncludeTimeValue=\'FALSE\' Type=\'DateTime\'>' + date + '</Value>' +
                                                '</Eq>' +
                                                '<Neq>' +
                                                    '<FieldRef Name=\'ID\' />' +
                                                    '<Value Type=\'Number\'>' + itemid + '</Value>' +
                                                '</Neq>' +
                                            '</And>' +
                                        '</Where>' +
                                    '</Query>' +

        sublistItems = activitiesList.getItems(subcamlQuery);


        clientContext.executeQueryAsync(onFilterQuerySucceeded, onQueryFailed);

function onFilterQuerySucceeded(sender, args) {
    subenumerator = sublistItems.getEnumerator();

    while (subenumerator.moveNext()) {
        currentSubListItem = subenumerator.get_current();

        substartTime = currentSubListItem.get_item('ActivityStartTime');
        subendTime = currentSubListItem.get_item('ActivityEndTime');
        subdate = new Date(currentSubListItem.get_item('ActivityDate'));
        subitemid = currentSubListItem.get_item('ID');


function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

However, I always get the error "The collection has not been initialized." right after this command: subenumerator = sublistItems.getEnumerator(); I have already made all variables global and checked my scopes. Obviously, I am missing something but I can't determine what. Can anybody help me here?

EDIT: I ended up using the server side object model with C# instead. If anybody know the answer, still feel free to add it.

  • is the activitiesList available in onFilterQuerySucceeded?
    – Gwny
    Commented Apr 8, 2015 at 13:37
  • yes it is available Commented Apr 8, 2015 at 13:41
  • why are you always creating a new SP Context?
    – Gwny
    Commented Apr 8, 2015 at 14:29
  • sorry that was just from something I tried. I took it out now as the problem persists whether I load the context anew or not. Commented Apr 8, 2015 at 14:43

1 Answer 1


I think your problem is that the variable sublistItems is re-assigned multiple times (in a while loop), so when onFilterQuerySucceeded is called the variable is most likely replaced with another value, which is not yet loaded.

One way to solve this is to pass sublistItems into onFilterQuerySucceeded as an argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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