First, the order you're attempting to perform the check vs the item creation in needs modified. You either need to check if a record will be valid, then submit it with separate actions -- or use something like an Exception Handling Scope to detect if the new item would violate your rules as it is being created. I think performing each action separately is probably the simplest method to explain and implement.
Second, you should try cleaning your code up first to solve some basic sources of problems:
- Your
ItemCreationSuccess
method only sets the text of some element
with an id of message
to the empty string which could be why you're not seeing any alerts or other behavior
- You also have a some scoping issues: you're setting
this.collListItem
equal to
targetList.getItems(camlQuery)
but then you call context.load
on
an item called collListItem
-- this.collListItem
is not the same object as collListItem
in your function. I would recommend reading up on some good answers about this
on StackOverflow as well as some on implied global variables to better explain what happens here.
Now let's tackle the first part of your problem; understanding if an item already exists that would overlap with a new request -- I used DateTime valued fields (like in the default SharePoint calendar list type) so that I can use numerical comparisons (greater than or less than) and highly suggest you do also instead of using text fields; you can convert them back and forth as you submit or display them on the page in your app.
function checkAvailabilityAsync(ctx, targetList, fromDate, toDate) {
// consider adding and returning a $.Deferred() object if you are using jQuery
var frDate = new Date(fromDate),
tDate = new Date(toDate),
camlQuery = new SP.CamlQuery(),
collListItem, strViewXml;
strViewXml = "<View><Query><OrderBy><FieldRef Name='EventDate'/></OrderBy><Where><And><Lt><FieldRef Name='EndDate'/>";
strViewXml += "<Value Type='DateTime'>" + tDate.toISOString() + "</Value></Lt><Gt><FieldRef Name='EventDate'/>";
strViewXml += "<Value Type='DateTime'>" + frDate.toISOString() + "</Value></Gt></And></Where></Query></View>";
camlQuery.set_viewXml(strViewXml);
collListItem = targetList.getItems(camlQuery);
ctx.load(collListItem);
ctx.executeQueryAsync(
// anonymous success handler
function() {
// you don't need to use 'createDelagate' here because the variables
// in the outer function are available here
if (collListItem.get_count() > 0) {
console.log('there is already an event on this day');
} else {
console.log('go ahead and create the event the user wants')
}
// resolve the jQuery.Deferred here if implemented
},
// anonymous failure handler
function(sender, args) {
alert("There was a problem running checkAvailability: \n" + args.get_message());
// reject the jQuery.Deferred here if implemented
}
);
// return the jQuery.Deferred.promise() here if implemented
}
Then to call this you would do something maybe in a separate function like this:
(function() {
var myCtx = SP.ClientContext.get_current(),
myList = myCtx.get_web().get_lists().getByTitle("Calendar"),
// notice 24 hour time format which is probably what you'll prefer
fromDate = "11 Nov 2014 14:00",
// or you can specify AM/PM like this
toDate = "11 Nov 2014 3:00 PM";
checkAvailabilityAsync(myCtx, myList, fromDate, toDate);
}());
Something to remember here, the new Date()
constructor used in the checkAvailability
function accepts many different types of date strings, so play around with it some in your browser dev tools javascript console to get a feel for which format you like best.
Anyway, I acknowledge that this isn't a complete solution, but I think this will get you headed in the right direction.
I also highly recommend taking some of the JavaScript classes on Code School (paid after the first few) or MS Virtual Academy (free -- particularly recommend "Javascript Fundamentals for Absolute Beginners")
newItem
,listItemCustom
, andlistItemFromTime
and where do they come from? Also, yourcamlQuery.set_viewXml
method doesn't insert the value of theresult
variable because the string isn't properly terminated -- the query string should be"...<Value Type='Text'>" + result + "</Value>..."
-- need a little more information to give an answer here :)onQuerySucceeded
in yourcreateItem
function). Also, it looks like you might have just accidentally added a duplicateexecuteQueryAsync
call -- since I'm assuming you only want/need to call and handle it one time.