This should, in theory, work. Sorry if it looks a bit daunting but what it's supposed to do is copy the event handler for both save buttons (in the ribbon and on the page body) and replace the "onclick" event with your custom function which in this case I called isUserAllowed
.
isUserAllowed
is a bit more complicated that it normally would because we have to send a query for what level of permissions does a user have. If you were just comparing the date you could've used something like the following:
function PreSaveAction(){
var shippingDate = $( '[Title="Shipment Day"]' ).value;
shippingDate = new Date( shippingDate );
var thresholdDate = new Date(); // Today
thresholdDate.setDate( thresholdDate.getDate() + 5 );
if(shippingDate > thresholdDate){return true}
else{return false}
}
Or you know... just the validation you've got already.But since you're looking at a query on permission levels all that logic gets put into the "success" function in the ctx.executeQueryAsync()
below.... If you need more help send me a message. I don't currently have my SharePoint environment to test further.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script>
<script type = "text/javascript">
// May potentially need to globally declare a couple variables...
var per = new SP.BasePermissions();
var originalSaveButtonClickHandler = function(){}
// https: //msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.permissionkind.aspx
// SP.PermissionKind.manageWeb
function isUserAllowed() {
// Client context for the web.
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
// Find the permission value in question.
var ob = new SP.BasePermissions();
ob.set( SP.PermissionKind.FullMask );
var per = web.doesUserHavePermissions( ob );
// Execure the query. On success, return if the user's permission is >= query.
// otherwise return false.
ctx.executeQueryAsync(
function() {
// Here's where you find the date field you're interested in.
// You may need to use the F12 screen or SP.ClientContext
// to determin the right value.
var shippingDate = $( 'input[Title="Shipment Day"]' )[0].value;
shippingDate = new Date( shippingDate );
// Get the value of Today + 5 days
var thresholdDate = new Date(); // Today
thresholdDate.setDate( thresholdDate.getDate() + 5 );
// the query result stored in per ( web.doesUserHavePermissions(ob) )
// should tell you if the user has the stated permissions level or not
// as a boolean value. Call "originalSaveButtonClickHandler()"
// in each statment you want to be valid. Use alerts or other methods
// to notify the user if their field is incorrect.
if ( per.get_value() ) {
originalSaveButtonClickHandler();
} else if(shippingDate > thresholdDate) {
alert( "Invalid Permissions" );
} else {
originalSaveButtonClickHandler();
}
},
function() { alert("Failed query."); }
);
}
function changeSaveAction() {
//gets form save button and ribbon save button
var saveButton = $( "[name$='diidIOSaveItem']" );
if ( saveButton.length > 0 ) {
//save original function
originalSaveButtonClickHandler = saveButton[ 0 ].onclick;
}
//change onclick to execute our custom validation function
$( saveButton ).attr( "onclick", "isUserAllowed()" );
}
// This auto runs the function "changeSaveAction" once all SharePoint related
// content has been loaded.
_spBodyOnLoadFunctionNames.push( "changeSaveAction" );
</script>