onsubmit
Posted: 11 Feb 2016
My code always return true,
fd.onsubmit(function () {
var checkDuplicate = itemIsDuplicate();
checkDuplicate
.done(function(isDuplicate) {
if(isDuplicate) {
return false;
}
});
return true;
});
function itemIsDuplicate() {
var deferred = $.Deferred();
var isDuplicate = false;
var ctx = SP.ClientContext.get_current(),
targetList = ctx.get_web().get_lists().getByTitle("BookingTest"),
frDate = fd.field('EventDate').control().value()[0].toString().substring(6,10)+"-"+fd.field('EventDate').control().value()[0].toString().substring(3,5)+"-"+fd.field('EventDate').control().value()[0].toString().substring(0,2)+"T"+fd.field('EventDate').control().value()[1].toString().substring(0,2)+":"+fd.field('EventDate').control().value()[2].toString()+":00Z",
tDate = fd.field('EndDate').control().value()[0].toString().substring(6,10)+"-"+fd.field('EndDate').control().value()[0].toString().substring(3,5)+"-"+fd.field('EndDate').control().value()[0].toString().substring(0,2)+"T"+fd.field('EndDate').control().value()[1].toString().substring(0,2)+":"+fd.field('EndDate').control().value()[2].toString()+":00Z",
camlQuery = new SP.CamlQuery(),
collListItem, strViewXml;
strViewXml = "<View><Query><Where><And><Lt><FieldRef Name='EventDate' />";
strViewXml += "<Value IncludeTimeValue='TRUE' Type='DateTime'>" + tDate + "</Value></Lt><Gt><FieldRef Name='EndDate' />";
strViewXml += "<Value IncludeTimeValue='TRUE' Type='DateTime'>" + frDate + "</Value></Gt></And></Where></Query></View>";
camlQuery.set_viewXml(strViewXml);
collListItem = targetList.getItems(camlQuery);
ctx.load(collListItem);
ctx.executeQueryAsync(
function(){deferred.resolve(collListItem.get_count() > 0); },
function(sender, args) {deferred.reject(sender, args.get_message()); }
);
return deferred.promise();
}
fd.onsubmit(function () {
var checkDuplicate = itemIsDuplicate();
checkDuplicate
.done(function(isDuplicate) {
if(isDuplicate) {
return false;
}
});
return true;
});
function itemIsDuplicate() {
var deferred = $.Deferred();
var isDuplicate = false;
var ctx = SP.ClientContext.get_current(),
targetList = ctx.get_web().get_lists().getByTitle("BookingTest"),
frDate = fd.field('EventDate').control().value()[0].toString().substring(6,10)+"-"+fd.field('EventDate').control().value()[0].toString().substring(3,5)+"-"+fd.field('EventDate').control().value()[0].toString().substring(0,2)+"T"+fd.field('EventDate').control().value()[1].toString().substring(0,2)+":"+fd.field('EventDate').control().value()[2].toString()+":00Z",
tDate = fd.field('EndDate').control().value()[0].toString().substring(6,10)+"-"+fd.field('EndDate').control().value()[0].toString().substring(3,5)+"-"+fd.field('EndDate').control().value()[0].toString().substring(0,2)+"T"+fd.field('EndDate').control().value()[1].toString().substring(0,2)+":"+fd.field('EndDate').control().value()[2].toString()+":00Z",
camlQuery = new SP.CamlQuery(),
collListItem, strViewXml;
strViewXml = "<View><Query><Where><And><Lt><FieldRef Name='EventDate' />";
strViewXml += "<Value IncludeTimeValue='TRUE' Type='DateTime'>" + tDate + "</Value></Lt><Gt><FieldRef Name='EndDate' />";
strViewXml += "<Value IncludeTimeValue='TRUE' Type='DateTime'>" + frDate + "</Value></Gt></And></Where></Query></View>";
camlQuery.set_viewXml(strViewXml);
collListItem = targetList.getItems(camlQuery);
ctx.load(collListItem);
ctx.executeQueryAsync(
function(){deferred.resolve(collListItem.get_count() > 0); },
function(sender, args) {deferred.reject(sender, args.get_message()); }
);
return deferred.promise();
}