Custom Dialog Return Value
-
06-12-2019 - |
Question
I have a custom page that I load in a SharePoint dialog. I call the javascript from an ASPX page, but when I close the dialog I want the javascript callback to "push" a value back to my ASPX page. Is this possible?
Solution 2
Here's what I've done:
function dialogCallback(result, value) {
if (result === SP.UI.DialogResult.OK) {
// find the control to set the value
var obj = document.getElementById(getRealId('MY_REALLY_UNIQUE_ID'));
if (obj != null) {
obj.value = value;
}
}
}
// Get the complete id of the control
function getRealId(partialid) {
var re = new RegExp(partialid, 'g');
var elems = document.getElementsByTagName('*'), i = 0, el;
while (el = elems[i++]) {
if (el.id.match(re)) {
return el.id;
}
}
}
It's was more an javascript thing than a Sharepoint one.
OTHER TIPS
Yes, certainly it's possible and can be done through dialogReturnValueCallback
parameter.
Sample code for popping up the dialog:
var options =
{
url: '/_layouts/CustomDialogPage.aspx',
title: 'My modal dialog',
dialogReturnValueCallback:
function (dialogResult, returnValue) {
if (dialogResult == SP.UI.DialogResult.OK) {
alert(returnValue);
}
}
};
SP.UI.ModalDialog.showModalDialog(options);
... and for returning the value (from CustomDialogPage.aspx):
window.frameElement.commitPopup('Hello from custom dialog page!');
If you're using postbacks and server-side code in your custom dialog page, you should use the following code to achieve this:
Response.Write("<script type='text/javascript'>window.frameElement.commitPopup('Hello from custom dialog page!');</script>");
Response.Flush();
Response.End();
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange