I am quite new to SharePoint development and am facing an issue in creating a custom calendar list allowing only one entry per user per day with entry only for mode of work (i.e. from home/office etc).
This is actually an employee timesheet. I am using SharePoint online and have no access to InfoPath forms or SharePoint Designer. I got one JavaScript code from: Limit only one entry per day per user in sharepoint list which I tried embedding in script editor, but its not working and showing no errors as such.
Can anyone help me out?
Here's the code:
var NameSpace = window.NameSpace || {};
var ItemCreation = NameSpace.ItemCreation || {};
ItemCreation.UserName = '';
ItemCreation.ListName = 'Timesheet';
var newListItem = '';
ItemCreation.CreatingItem = (function () {
CreateItem = function() {
var clientContext = new SP.ClientContext.get_current();
/* Get current web */
var oWeb = clientContext.get_web();
/* Get list by title */
var oList = oWeb.get_lists().getByTitle(ItemCreation.ListName);
/* Create new object for list item creation */
//
var oListItemCreationInfo = new SP.ListItemCreationInformation();
newListItem = oList.addItem(oListItemCreationInfo);
newListItem.set_item('Mode of Work');
newListItem.update();
clientContext.load(newListItem);
clientContext.executeQueryAsync(onSuccess, onFailure);
},
Validate = function() {
var clientContext = new SP.ClientContext.get_current(),
web = clientContext.get_web(),
list = web.get_lists().getByTitle(ItemCreation.ListName),
camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><And><Eq><FieldRef Name="Employee Name" /><Value Type="User">"+ ItemCreation.currentUser +"</Value></Eq><Eq><FieldRef Name="Date" /><Value Type="Calculated">" + ItemCreation.Date + "</Value></Eq></And></Where></Query></View>');
ItemCreation.Item = list.getItems(camlQuery);
clientContext.load(ItemCreation.Item);
clientContext.executeQueryAsync(onValidateSuccess, onFailure);
},
onValidateSuccess = function() {
if (ItemCreation.Item.get_count() > 0) {
alert("You can not create more than one item in a day");
}
else {
CreateItem();
}
},
getCurrentUser = function () {
try {
var clientContext = new SP.ClientContext.get_current();
var tempcurrentUser = clientContext.get_web().get_currentUser();
clientContext.load(tempcurrentUser);
clientContext.executeQueryAsync(function () {
var index = tempcurrentUser.get_loginName().indexOf('|') + 1;
ItemCreation.currentUser = tempcurrentUser.get_loginName().substring(index);
Validate();
}, onFailure);
}
catch (err) {
onFailure();
}
},
onSuccess = function() {
alert("Success");
},
onFailure = function() {
alert("Error");
}
return {
getCurrentUser: getCurrentUser
};
}());
$(document).ready(function () {
SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function () {
SP.SOD.executeFunc('user', 'SP.UserProfiles', function () {
ItemCreation.CreatingItem.getCurrentUser();
console.log("Script loaded");
});
});
});
P.S. : The console output inside the document.ready()
function is also not displayed.