Pass arguments from/to Modal Dialog
By Anatoly Mironov
To pass arguments from a ModalDialog is easy. Provide some buttons and invoke close function:
SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, someValue);
```The first argument is result, [it is a enumeration with three alternatives](http://msdn.microsoft.com/en-us/library/ff409060.aspx): cancel, invalid and OK. The value you pass back to the main window can be a simple string, or a complex javascript object. In this example I'll create a html element which is hidden (id="modal-form" [class="s4-die"](/2011/10/14/s4-die/)):
Pass arguments to Modal Dialog and use it
In options you pass to SP.UI.ModalDialog.showModalDialog you can define args property. It can be any object with any complexity.
var options = {
//...
args: {
title: "title from arguments"
}
};
SP.UI.ModalDialog.showModalDialog(options);
```To use it in a modal dialog, [you have to get the args this way](http://sharepoint.stackexchange.com/questions/31472/what-arguments-can-i-pass-to-newform-aspx-using-sp-ui-modaldialog-showmodaldialo/31474#31474 "See an example on sharepoint.stackexchange"):
var args = SP.UI.ModalDialog.get_childDialog().get_args();
ExecuteOrDelayUntilScriptLoaded(function(){ var args = SP.UI.ModalDialog.get_childDialog().get_args(); var title = args.title; console.log(title); }, “sp.js”);
## Comments from Wordpress.com
####
[jess](http://none "jess.vermont.stl@gmail.com") - <time datetime="2012-08-10 17:29:45">Aug 5, 2012</time>
How do I get the args object I assigned to the ModalDialog BACK? Basically I have a modal dialog box which I create with an args object attached to it. I want to pass those arguments into the modal dialog box (already done) and, if the user does not select something on the Modal Dialog box, I need to RETURN the same values that I passed in. How do I reference the args object from inside of a JavaScript "cancel" or "close window" method OF THE MODAL DIALOG ITSELF? function cancelForm() { var dialogResult = SP.UI.DialogResult.OK; var args = SP.UI.ModalDialog.get\_childDialog().get\_args(); var returnValue = Array(); returnValue\[0\] = SP.UI.ModalDialog.get\_childDialog().get\_args("SupervisorCn"); returnValue\[1\] = SP.UI.ModalDialog.get\_childDialog().get\_args("Supervisor"); SP.UI.ModalDialog.commonModalDialogClose(dialogResult, returnValue); } I'm getting "undefined" when I check the value of returnValue\[0\] after this assignment takes place. Also "args" is "undefined" after the call to "get\_args()" as well. Any feedback you can offer is appreciated.
<hr />
####
[Anatoly Mironov]( "mirontoli@gmail.com") - <time datetime="2012-08-10 18:51:34">Aug 5, 2012</time>
Hi, Jess. I can't test it now, but I think, you should get the args first, and then get your properties:
var returnValue = []; // [] is the same as Array(); var args = SP.UI.ModalDialog.get_childDialog().get_args(); returnValue[0] = args[“SupervisorCn”]; returnValue[1] = args[“Supervisor”];
returnValue[“SupervisorCn”] = args[“SupervisorCn”]; returnValue[“Supervisor”] = args[“Supervisor”];
<hr />
####
[Jess]( "jess.vermont.stl@gmail.com") - <time datetime="2012-08-10 19:03:36">Aug 5, 2012</time>
Hello Anatoly and thank you for your rapid response. I am off work and offsite until Monday so cannot try this but will do so when I return first thing Monday morning. I appreciate your help. Have a good weekend.
<hr />
####
[Anatoly Mironov]( "mirontoli@gmail.com") - <time datetime="2012-08-10 20:58:42">Aug 5, 2012</time>
You're welcome! I am actually off work, too. On Monday I am at work so I can test it and try it, too.
<hr />
####
[Sachin]( "sachin.suresh@outlook.com") - <time datetime="2013-01-10 16:16:18">Jan 4, 2013</time>
Hey Anatoly, I tried doing the same thing you have done here, but instead of the input value I keep getting the \[object Object\] as a result... I tried attaching a .toString() the property, but that didn't work either, have you seen this before or do you have any ideas?
<hr />
####
[Anatoly Mironov]( "mirontoli@gmail.com") - <time datetime="2013-01-11 16:07:50">Jan 5, 2013</time>
Hi! How do you output this property? running alert of an object would result in \[object Object\]. But it doesn't mean you get "\[object Object\]". Maybe it works fine. Try console.log in a web browser (but not in IE :) ).
<hr />
####
[Nolan Shaw](https://www.nolanshaw.com/ "nolanshaw794@gmail.com") - <time datetime="2021-10-08 16:05:26">Oct 5, 2021</time>
Awesome blog yoou have here
<hr />