Monday, 11 September 2017

Cannot Read Property 'Showmodaldialog' of undefined

While working with SharePoint / JavaScript modal pop up you might encounter error "cannot read property 'showmodaldialog' of undefined". Here is simple solution for that.

Code Sample:

function OpenDocsDialog(url1) {
    var options = {
        url: url1,
        dialogReturnValueCallback: myDialogCallback
    };
        SP.UI.ModalDialog.showModalDialog(options);  // this line gives error.
}


Solution :  We need to load "sp.js" file before calling above code. You can load "sp.js" file with below code sample:

ExecuteOrDelayUntilScriptLoaded(function () { //code }, "sp.js")

Full solutionExample :

function OpenDocsDialog(url1) {
    var options = {
        url: url1,
        dialogReturnValueCallback: myDialogCallback
    };

    ExecuteOrDelayUntilScriptLoaded(function () {
        SP.UI.ModalDialog.showModalDialog(options);
    }, "sp.js")
}

2 comments:

  1. This article provides a helpful solution to the SP.UI.ModalDialog.showModalDialog error encountered when working with SharePoint and JavaScript. Explaining how to load sp.js using ExecuteOrDelayUntilScriptLoaded() is a practical way to address script-loading dependencies and ensure the SharePoint modal dialog API is available before it is called.

    Understanding JavaScript execution, asynchronous script loading, and API dependencies is important when developing reliable web applications. Exploring Javascript Online Training in India can help developers strengthen their JavaScript fundamentals and learn practical debugging techniques.

    ReplyDelete
  2. Developers can also benefit from structured learning that covers JavaScript functions, browser APIs, and event-driven programming. A Javascript Online Course in India can provide a foundation for understanding script execution and resolving common issues in web application development.

    ReplyDelete

The "Mark as Decorative" Feature in SharePoint: A Small Toggle With a Big Accessibility Impact

If you've spent any time building pages in SharePoint Online, you've probably noticed a new checkbox tucked inside the Accessibility...