I tried to close a custom .aspx dialog in SharePoint 2013 but I will get this failure message:

"Unable to get property 'commonModalDialogClose' of undefined or null reference".

Can someone tell me where my problem is, please?

My C# for the .aspx page to close:

protected void ButtonOK_OnClick(object sender, EventArgs e)
    string[] isDlg = this.Request.QueryString.GetValues("IsDlg");

    if (isDlg != null && (isDlg.Count() == 1 && isDlg[0] == "1"))
        Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", "SP.UI.ModalDialog.commonModalDialogClose(1, 1);", true);

2 Answers 2


Order of JavaScript code execution is the issue. When your code is executed, window.SP.UI.ModalDialog is still undefined. It's because sp.js file that register ModalDialog api is executed after (or never, it depends on masterpage). Some of the solutions are:

  1. Register your script later(for example, replace call to RegisterStartupScript with RegisterClientScriptBlock) or

  2. (preferred one) Wrap your JavaScript-code with ExecuteOrDelayUntilScriptLoaded(function(){ SP.UI.ModalDialog.commonModalDialogClose(1, 1);}, 'sp.js'});

This is SharePoint "way" to resolve JavaScript dependencies.



<script src="/_layouts/SP.UI.Dialog.js" type="text/javascript" />
  • 1
    Don't add links to your blog to your post - this is considered spam.
    – Glorfindel
    Commented Apr 4, 2017 at 6:45
  • If you're referencing your own blog you should (1) clearly state that this is your own blog and (2) put all relevant content to this answer. If you're blog moves to a new URL, this post would be invalid. Thank you.
    – Benny Skogberg
    Commented Apr 4, 2017 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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