SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to open the sharepoint 2010 javascript modal dialog from code behind? I have tried..

        System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">");
        System.Web.HttpContext.Current.Response.Write("var options =");
        System.Web.HttpContext.Current.Response.Write("    url: 'ok.aspx',");
        System.Web.HttpContext.Current.Response.Write("    width: 100,");
        System.Web.HttpContext.Current.Response.Write("    height: 100,");
        System.Web.HttpContext.Current.Response.Write("    allowMaximize: false,");
        System.Web.HttpContext.Current.Response.Write("    title: 'HATA'");
        System.Web.HttpContext.Current.Response.Write("modalDialog = SP.UI.ModalDialog.showModalDialog(options);");

.. and it fails.

share|improve this question
What error are you getting exactly? – Waldek Mastykarz - MVP Jun 16 '11 at 21:42
No error, the dialog just doesn't display – user601653 Jun 18 '11 at 10:52
up vote 3 down vote accepted

I'm guessing you want the dialog to open at page load, not as a result of a client-side button press (which is a little easier). You have to make sure the Sharepoint ECMA-libraries are loaded. It's easiest to use jQuery to register the opening of the dialog as a load event; if you use regular javascript in some browsers your load event will take place before all other load events, and it might fail because of it.

This would be your JS code:

function openDialog() {
    var options = {
        url: 'ok.aspx',
        tite: 'HATA',
        allowMaximize: false,
        showClose: true,
        width: 100,
        height: 100,
        dialogReturnValueCallback: callback_openDialog

    function callback_openDialog(dialogResult, returnValue) {
        console.log('openDialog result: ' + dialogResult);
        if (dialogResult != SP.UI.DialogResult.Cancel) {

Use RegisterClientScriptBlock to include as a string or put the code in a .js file and include using RegisterClientScriptInclude.

To include JQuery add a mapped folder to layouts and add the JQuery file to it. Use the statement below to include it in your page.

Page.ClientScript.RegisterClientScriptInclude("JQuery", string.Format("{0}/_LAYOUTS/jquery-1.5.1.min.js", web.Site.Url));

I don't know if dialogReturnValueCallback is required, but since MSDN isn't exactly reliable in this sort of thing, I'm not sure if you can omit it.

Use the Firebug console, or the IE F12 console to debug your javascript, and if you have any problems with this let us know. (Please include error-messages if possible)

share|improve this answer

If you want to inject javascript onto your page, you can use RegisterClientScriptBlock

ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(this.GetType(), "your-key", "the text for your script", true);
share|improve this answer

You can use OpenPopUpPageWithTitle method to open dialog in Sharepoint 2010.

share|improve this answer
please dont include curtesy phrases in questions or answers as it clutters up the site :-) – Anders Rask Jun 17 '11 at 14:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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