1

I have a list where user's can't edit a column if

=[Delivery Date/Time]>(TODAY()+5) 

I have this validation on Delivery Date/Time column.

I need admin/site owner needs to edit the list no matter what the delivery date/time is.

Please let me know how can I achieve this??

Thanks,

enter image description here

4
  • Since they're owners/administrators they could just go in, copy the validation and remove it from the column, make their change, then re add the validation. Not ideal, i know... But AFAIK there isn't an OOTB solution for this.
    – KGlasier
    Commented Nov 29, 2018 at 20:37
  • site owners doesn't have that much technical knowledge they are looking for easy solution.
    – H.Kumar
    Commented Nov 29, 2018 at 20:39
  • In that case if you're savvy with Javascript you could remove the validation and use your own validation via some Javascript and query the current user's permission levels. I can try and get a code sample that'll work as a basis, but it may take a bit for me to figure out. It would have to go on both the new and edit display forms.
    – KGlasier
    Commented Nov 29, 2018 at 20:42
  • I've some knowledge with JavaScript any code sample will be great and thanks for your help on this.
    – H.Kumar
    Commented Nov 29, 2018 at 20:45

1 Answer 1

1

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>

Based largely on this answer

2
  • I think I'll create a workflow, I've very limited skills with java script but thanks for your help
    – H.Kumar
    Commented Nov 30, 2018 at 20:04
  • Okay, good luck.
    – KGlasier
    Commented Nov 30, 2018 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.