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();
camlQuery.set_viewXml('<View><Query></Query></View>');
listItems = activitiesList.getItems(camlQuery);
clientContext.load(listItems);
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>' +
'</View>');
sublistItems = activitiesList.getItems(subcamlQuery);
clientContext.load(sublistItems);
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.