Montag, 30. März 2015

ExecuteOrDelayUntilScriptLoaded not working

Problem:
ExecuteOrDelayUntilScriptLoaded in a page or specifically in a publishing page does not work.
This might be also the case when a user who has only read/view access to the site, sp.js refuses to load when ExecuteOrDelayUntilScriptLoaded is used.

Solution:
In SharePoint 2013, the correct way to execute a function after script is loaded is to use SP.SOD class and not ExecuteOrDelayUntilScriptLoaded.
For example I needed to use it in:
 
function openInDialog(dlgWidth, dlgHeight, dlgAllowMaximize, dlgShowClose, pageUrl ) {
      var options = {
          url: pageUrl,
          width: dlgWidth,
          height: dlgHeight,
          allowMaximize: dlgAllowMaximize,
          showClose: dlgShowClose
      };

      options.dialogReturnValueCallback = Function.createDelegate(null, CloseDialogCallback);
      SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);
  }


  function CloseDialogCallback(dialogResult, returnValue) {
      if (dialogResult == SP.UI.DialogResult.OK) { // refresh parent page
          SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.RefreshPage', SP.UI.DialogResult.OK);
      }
      // if user click on Close or Cancel
      else if (dialogResult == SP.UI.DialogResult.cancel) { // Do Nothing or add any logic you want
      } else { //alert("else " + dialogResult);
      }
  }

Keine Kommentare:

Kommentar veröffentlichen