0

I have made a custom sharepoint site, where i have disabled the remove button from the ribbon and the context menu. But i just found out that then if I select an item in a document library, i can still press the delete key on the keybord and it will delete the item from the document library i will like to disable the delete key on the keyboard on the sharepoint site; how can this be done ?

1

yes you can but from the top of my head using javascript and cewp would be a simple solution.

add this in the cewp:

$('html').bind('keypress', function(e) 
{ 
   if(e.keyCode == 46) 
   { 
      return false; 
   } 
}); 

taken from here:

https://stackoverflow.com/questions/1235716/disable-keyboard-enter-key

i just added the right keycode in for you :) , you would obviusly need to put the cewp on the document library page ;)

EDIT

Ok just relised the code above wont work as it would be listing for an input so Iv dont the code for you! Its simple really and doesnt require you to assign the function to a input box or such, it check to see if your in a textbox and if you are then delete button works as normal otherwise its disabled :)

<script type="text/javascript" language="javascript">  
$(document).keydown(function(e) {
var element = e.target.nodeName.toLowerCase();
if (element != 'input' && element != 'textarea') {
    if (e.keyCode === 46) {
        return false;
    }
}
});
</script> 

If you dont want that feature and want to disable it all together than add the following:

<script type="text/javascript" language="javascript">  
$(document).keydown(function(e) {
var element = e.target.nodeName.toLowerCase();
    if (e.keyCode === 46) {
        return false;
    }
});
</script>

hope it helps!

4
  • 1
    the metode is not working in sharepoint it is stil running the delete command Aug 27 '12 at 8:27
  • the bottom two examples 100% work as iv tested them myself using cewp, one doesnt allow delete key press but allows it on a textbox and the second one doesnt allow the delete key atall!
    – Ali Jafer
    Aug 27 '12 at 10:10
  • i have just testet the code and it is getting run but it is still delete the folder from a document library i is like it is running the delete before the script Aug 28 '12 at 8:26
  • ok sounds like a big issue and you are right! The only other suggestion is to make an eventReciver that checks on item deleting and if the user doesnt have the required privlages then stop the deletion else delete, here is a good example: social.technet.microsoft.com/Forums/en-US/…
    – Ali Jafer
    Aug 28 '12 at 9:12
1

I suggest to attach an event handler directly to the checkboxes. It works both on modern and classic pages.

$(document).ready(function () {
    $("[role=checkbox]").each(function (index, el) {
        el.addEventListener('keydown', function (event) { 
            if (event.which == 46) { event.stopPropagation(); } 
        });
    });
});

https://zsvipullo.blogspot.com/2019/02/disable-canc-keyboard-button-to-avoid.html

0

Another ways, you just modify current view, scroll to Tabular View => uncheck Allow individual item checkboxes => Save. This can prevent user delete row by delete key, but user cannot select multirow in table.

-1

Now, I find out the way to disable event delete on list view simply by edit code in core.js (..Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS).

Find the keyword "case Sys.UI.Key.del:" and comment HandleItemDelete like this.

function OnListViewKeyDown(ei, ctxNum) {
    var ctxCur = GetListContextFromContextNumber(String(ctxNum));

    if (ctxCur == null || ctxCur.inGridMode) {
        return;
    }
    var keyCode = ei.keyCode;
    var fShiftOrCtrlPressed = ei.shiftKey || ei.ctrlKey;

    if (IsCallOutOn()) {
        return;
    }
    switch (keyCode) {
    case Sys.UI.Key.del:
        /*HandleItemDelete(ei, ctxNum);*/
        break;

save and try.

1
  • 1
    You should really avoid modifying code in the hive(s). It is certainly not recommended by Microsoft, and your changes can be overwritten by any update made from Microsoft Jun 1 '14 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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