SP.UI.ModalDialog.showModalDialog() Does Not Work Under SharePoint 2013

Introduction

Have you ever tried using SP.UI.ModalDialog.showModalDialog(options) in SharePoint 2013? I discovered a strange behaviour.

After migrating my code from SharePoint 2010 to SharePoint 2013 the calls to showModalDialog failed with the message that the method cannot be found (JavaScript). When checking it in IE Developer Tools this isn't surprising at all. The required js-file isn't loaded.

But why? I guess it must be the new Script on Demand (SOD) Model that was introduced in SharePoint 2013.

SharePoint 2010 Example

  1. function ShowServerInformation() {  
  2. var options = {  
  3. url: '/_layouts/Management/GeneralInformation.aspx',  
  4. tite: 'Management Information',  
  5. allowMaximize: false,  
  6. showClose: true,  
  7. width: 430,  
  8. height: 230  
  9. };  
  10. SP.UI.ModalDialog.showModalDialog(options);  
  11. return false;  
  12. }  
It's very easy to fix this problem.

Remove the Java Script reference.

 

  1. <script src="/_layouts/sp.js" type="text/javascript"></script>
  2. <script src="/_layouts/SP.UI.Dialog.js" type="text/javascript"></script>
  3. Add to the url variable "?IsDlg=1"`
  4. Replace the command SP.UI.ModalDialog.showModalDialog() with the new command
  5. SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);

After this few changes your solution will work correctly.

SharePoint 2013 Example

  1. function ShowServerInformation(featureId) {  
  2.   
  3.  var options = {  
  4.   url: '/_layouts/Management/GeneralInformation.aspx?IsDlg=1',  
  5.           tite: 'Management Information',  
  6.            allowMaximize: false,  
  7.             showClose: true,  
  8.             width: 430,  
  9.              height: 230  
  10.        }  
Notice

I first tried the "executeOrDelayUntilScriptLoaded" function, but it was not much help. It just "swallowed" my function call but never executed it because the js-file I specified was never loaded.