Modal Dialog in SharePoint
there are certain business conditions which requires to show modal popups and let the action decided by user.
I already have written a post to show how to show wait screens in SharePoint. sp.ui.dialog.js in SharePoint is responsible for showing modal dialogs also.
there is a method actually there are more, the one which looks most convenient to me is :
SP.UI.ModalDialog.showModalDialog(options)
only point you need to make sure is sp.js and sp.ui.dialog.js are loaded. one can use ExecuteOrDelayUntilScriptLoaded
all you need to do is to create options and pass argument to modal dialog. options is created as below.
var newPopUp = SP.UI.$create_DialogOptions();
now you can set url to open as dialog , title , height, width and many other thing please have a look on msdn link given below. here the important point is you can pass a callback function that can be called when dialog is closed.
dialog is closed by following method
SP.UI.ModalDialog.commonModalDialogClose(dialogresult,url); dialog result can be SP.UI.DialogResult.cancel or SP.UI.DialogResult.invalid or SP.UI.DialogResult.OK also you can pass a url to callback method using url parameter.
lets define a method to open the dialog with callback fuction and a method for opening dialog.
function openModalPopup(url, title, width, height,callbackfunction)
{
// create options
var newPopUp = SP.UI.$create_DialogOptions();
newPopUp.url =url;
if (title != null)
newPopUp.title = title;
if (width != null)
newPopUp.width = width;
if (height != null)
newPopUp.height = height;
//set callback function
newPopUp.dialogReturnValueCallback = Function.createDelegate(null, callbackfunction);
//show popup
SP.UI.ModalDialog.showModalDialog(newPopUp);
}
now our function is ready to show modal dialog. now lets create a function and pass actual parameters to show the modal dialog.
function showpopup()
{
var url =<url of your page>;
//call our above method and define callback
openModalPopup(url,"Test Popup",500,500,function(result,target){
switch (result) {
case SP.UI.DialogResult.invalid, -1:
// invalid has been passed from commonModalDialogClose
SP.UI.Status.removeAllStatus(true);
var sidErr = SP.UI.Status.addStatus("there was an error while performing task", target, true);
SP.UI.Status.setStatusPriColor(sidErr, "yellow");
break;
case SP.UI.DialogResult.cancel, 0:
// dialog has been closed or cancelled
SP.UI.Status.removeAllStatus(true);
var sidErr = SP.UI.Status.addStatus("task was cancelled", target, true);
SP.UI.Status.setStatusPriColor(sidErr, "green");
break;
case SP.UI.DialogResult.OK, 1:
// operation was done and ok
SP.UI.Status.removeAllStatus(true);
var sid = SP.UI.Status.addStatus("task performed successfully.", "", true);
SP.UI.Status.setStatusPriColor(sid, "green");
// if you want you can refresh the page lets say after one seconds.
setTimeout(
function() {
SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
}, 1000);
break;
}
});
}
now the code is ready for showing modal popup just call showpopup method.
we should think how we will close the popup. so if you are opening an out of the box page then you will not write any thing.
but when you create your own page say in layouts then you define your OK and Cancel buttons
on click of cancel just make a call to commonModalDialogClose with cancel option as below
function closepopuppage() {
// should be written in popup page
// should be written in popup page
SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.cancel, _spPageContextInfo.webAbsoluteUrl);
}
similarly on click of OK you will perform your task , close the dialog using SP.UI.DialogResult.OK option and in case of exceptions you will pass invalid option.
in all cases you can register script from server side to close the popup( in the cs file of popup).
for ex.
string script = "var url='" + SPContext.Current.Web.Url +
"';SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK,url);";
Page.ClientScript.RegisterStartupScript(GetType(), "testscript", script, true);
for all the option values you can have a look on msdn.
https://msdn.microsoft.com/en-us/library/office/ff410058%28v=office.14%29.aspx?f=255&MSPPError=-2147217396