Override Default Cancel Button On Ribbon

This time, I ran into a requirement, where I have to override the default cancel button on a SharePoint form on the ribbon under Edit tab in the commit section. I would like to share my knowledge on this and be of some help there by saving your time.

Here, I am removing the default Cancel button from the ribbon and adding a new custom Cancel button in the same place with the same classes and the same image.

The only thing is when a Cancel button is clicked; a certain set of operations were performed before redirecting the page back to the source.

Here, I have demonstrated this just by showing a confirmation box on clicking Cancel button and redirecting the form back to the list view.

Cancel button is given below..

Following Cancel button, we have the confirmation box, which appears on hitting Cancel.

To achieve this, we have a code given below, which can be used as it is.

In the document.ready, I have called the overrideCancel() function.

  1. $(function () {  
  2.     overrideCancel();  
  3. });  

 

Inside the override function, I have taken care of the required classes, which is required by Cancel button. I creates all the required elements on runtime, which will hold the button.

Out of these elements we have the image element. You have to get an image of Cancel button and save it somewhere in the site. Therefore, we specify the path of the image in the code.

  1. function overrideCancel() {  
  2.     var input = document.createElement('a');  
  3.     input.title = "Cancel";  
  4.     input.role = "button";  
  5.     input.id = "custominput";  
  6.     $('.ms-cui-group:first').find('.ms-cui-row-onerow:first').append(input)  
  7.     $("#custominput").addClass("ms-cui-ctl-large");  
  8.   
  9.     var firstSpan = document.createElement('span');  
  10.     firstSpan.id = "fSpan";  
  11.     $("#custominput").append(firstSpan);  
  12.     $("#fSpan").addClass("ms-cui-ctl-largeIconContainer");  
  13.   
  14.     var innserSpan = document.createElement('span');  
  15.     innserSpan.id = "iSpan";  
  16.     $("#fSpan").append(innserSpan);  
  17.     $("#iSpan").addClass("ms-cui-img-32by32");  
  18.     $("#iSpan").addClass("ms-cui-img-cont-float");  
  19.     $("#iSpan").addClass("ms-cui-imageDisabled");  
  20.   
  21.     var cancelImg = document.createElement('img');  
  22.     cancelImg.src = _spPageContextInfo.webAbsoluteUrl + "/Style Library/CSS/Images/CancelRibbon.png";  
  23.     $("#iSpan").append(cancelImg);  
  24.   
  25.     var lbl = document.createElement('span');  
  26.     lbl.id = "lblSpan";  
  27.     $("#custominput").append(lbl);  
  28.     $("#lblSpan").addClass("ms-cui-ctl-largelabel");  
  29.     $("#lblSpan").text("Cancel");  
  30.     $("#custominput").click(function () { Cancel() });  
  31. }  

 

Finally, I have written the code for Cancel function call, which is when called will display the confirmation box and on OK, click the form, which will redirect otherwise on clicking Cancel. It is just the confirmation box, which is closed and the form remains in the same state.

  1. function Cancel() {  
  2.     var url = _spPageContextInfo.webAbsoluteUrl + "/Lists/ListName/AllItems.aspx";  
  3.     if (confirm("You have clicked on the overridden Cancel button, are you sure you want to close this form?")) {  
  4.         window.location = url;  
  5.     }  
  6.     else {  
  7.         return false;  
  8.     }  
  9. }  

 

Hope, this helps everyone to achieve something of this kind.

Ebook Download
View all
Learn
View all