Passing Data To and From SharePoint Modal Dialogs

Posted: Eric | Comments: 8 | June 13th, 2011
Jun 13

SharePoint Modal Dialogs

SharePoint 2010 introduced the ECMAScript Client Object Model. One of the things that it included was the Dialog Framework. This allows modal dialogs to be easily displayed inside of SharePoint. This is typically done by specifying a web page to be displayed as the dialog on top of the current web page. In this first example, a page named BasePage.aspx is displaying a dialog that is popupForm.aspx.

image

Displaying Dialogs

It is fairly trivial to display a dialog. All you have to do is initialize an options object and call SP.UI.ModalDIalog.showModalDialog. In this example, the page popupForm.aspx will be displayed as the modal dialog. When the dialog is closed, the method named “callbackMethod” will be called.

Displaying a Dialog
  1. var options = {
  2. url: ‘popupForm.aspx’,
  3. args: null,
  4. title: ‘Select Value’,
  5. dialogReturnValueCallback: dialogCallback,
  6. };
  7. SP.UI.ModalDialog.showModalDialog(options);

Passing Arguments in the URL

In many cases, you will want to pass a value to the dialog. The first way to do this is to pass it in as a query string. All that needs to be done is to append the appropriate item and value to the URL. In this example, “7” would be passed to the dialog in a query string named “ItemID”.

Argument in the Query String
  1. var options = {
  2. url: ‘popupForm.aspx?itemID=7′,
  3. args: null,
  4. title: ‘Select Value’,
  5. dialogReturnValueCallback: dialogCallback,
  6. };
  7. SP.UI.ModalDialog.showModalDialog(options);

The value would be retrieved by the dialog in the code behind just as any query string would be retrieved. In this example, the code behind written in C#.

Retrieving the Argument
  1. int itemID = Convert.ToInt32(Request.QueryString["ItemID"]);

Passing Arguments in the Options Object

The other way to pass arguments to the use the args field in the options object. In this example, a value of 7 would be passed to the dialog as an argument.

Argument in the Options Object
  1. var options = {
  2. url: ‘popupForm.aspx’,
  3. args: 7,
  4. title: ‘Select Value’,
  5. dialogReturnValueCallback: dialogCallback,
  6. };
  7. SP.UI.ModalDialog.showModalDialog(options);

The value would be retrieved in javascript using the window.frameElement.dialogArgs property.

Retrieving the Argument
  1. var itemID = window.frameElement.dialogArgs;

Returning a Value from the Modal Dialog

When the user closes the modal dialog, at least one value is returned to the base form. This value is passed to the callback function specified in the options object. This value is the dialog result. The value will depend on how the dialog was closed.

  • If the dialog was closed by calling window.frameElement.cancelPopUp, the result will be SP.UI.DialogResult.cancel.
  • If the dialog was closed by called window.frameElement.commitPopup, the result will be SP.UI.DialogResult.ok.
  • If the dialog was closed with window.frameElement.commonModalDialogClose, the result is specified in the first parameter.

Side Note: The method named cancelPopUp capitalizes the ‘U’. The method named commitPopup does not.

Here is an example of how the callback would handle the result in JavaScript.

Handling the Callback
  1. function callbackMethod (dialogResult, returnValue) {
  2.     if(dialogResult == SP.UI.DialogResult.ok){
  3.         // The user clicked the OK button.                 
  4.         // handle commitPopup         
  5.     } else{
  6.         // The user clicked the Cancel button.                 
  7.         // handle cancelPopup         
  8.     }
  9. }

If the dialog needs to be closed from the code behind and not from client side code, the JavaScript is simply inserted in the response.

Closing The Dialog
  1. private void CloseDialog()
  2. {
  3. Context.Response.Write(“<script type=’text/javascript’>window.frameElement.commitPopup();</script>”);
  4. Context.Response.Flush();
  5. Context.Response.End();
  6. }

Returning an Argument

If you want to return an argument from the dialog, it is passed as the first parameter to the commitPopup. In this example, the dialog would return 7.

Returning a Value
  1. private void CloseDialog()
  2. {
  3. string response
  4. = string.Format(“<script type=’text/javascript’>window.frameElement.commitPopup(\”{0}\”);</script>”, 7);
  5. Context.Response.Flush();
  6. Context.Response.End();
  7. }

The value is received in the second parameter of the callback method. In the following example, the return value is placed into a hidden field on the base page.

Handle Return Value
  1. function callbackMethod (dialogResult, returnValue)
  2. {
  3. if (returnValue != null) {
  4. $get(‘<%= this.hiddenField.ClientID %>’).value = returnValue;
  5. }
  6. return;
  7. }
8 comments
  1. Lexus says:

    I found just what I was ndeeed, and it was entertaining!

  2. Herman says:

    Hi Please help
    How can i pass a textbox value from mainpages to a label on the SP.UI.Dialog

    • Eric says:

      There are two ways you could move data from the textbox on the main form to the label on the popup. You could pass the text of the textbox as a query string to the popup. Then you could set the text of the label in the Page_Load method of the popup page. This would be very similar to the ‘Passing Arguments in the URL’ section of the post.

      If you wanted to do so, you could handle the whole thing in javascript. This would follow the ‘Passing Arguments in the Options Object’ section of the post. I’ll assume you are using jQuery.

      Let’s say that your main page has a text box named txtInput. You could modify the code labeled ‘Argument in the Options Object’ to be the following:

      function DisplayDialog() {

      var options = {
      url: ‘PopupFormExample.aspx’,
      args: $(‘#txtInput’).val(),
      title: ‘Select Value’,
      dialogReturnValueCallback: callbackMethod
      };
      SP.UI.ModalDialog.showModalDialog(options);
      }

      This passes the text from the txtInput textbox to the dialog. Then in the document ready function of the popup page, you could set the label text to be the text from the argument.

      $(“span[id$='lblOutput']“).html(window.frameElement.dialogArgs);

      This code sample assumes that the label is named lblOutput.

  3. Herman says:

    Sorry i use a webpart and call the Dialog from there

  4. Endre says:

    Nice work. Simple and clean!

  5. Opening a Modal Dialog on Page Load from Server-Side Code? Don't Forget This Sweet (and Totally Critical) Function Call - SharePoint Development says:

    [...] my esteemed colleague Eric has previously detailed for us, SharePoint 2010 comes equipped with a snappy little API for working with modal popup dialogs, [...]

  6. Pass arguments from Modal Dialog « Sharepoint… says:

    [...] tip for further reading: SharePoint Development blog. Advertisement LD_AddCustomAttr("AdOpt", "1"); LD_AddCustomAttr("Origin", "other"); [...]

  7. Passing Multiple arguments to pop up dialog box and retrive the same in popup says:

    HI,
    Great Post,
    How Can I pass multiple arguments and retrive the same.
    below example passess only single parameter
    1.var options = {
    2. url: ‘popupForm.aspx’,
    3. args: 7,
    4. title: ‘Select Value’,
    5. dialogReturnValueCallback: dialogCallback,
    6. };
    7.SP.UI.ModalDialog.showModalDialog(options);

Leave a Comment