Thursday, April 26, 2012

SharePoint Modal Dialog


How to use SharePoint Modal Dialog i.e. SPModalDialog

Here are some of the common functions related with this

To open a dialog use the below function in javascript on the html part of the page

function NewItemDialog(url, title) {
        var options = {
            url: url,
            width: 900,
            height: 700,
            title: title,
            dialogReturnValueCallback: DialogCallback
        };
        SP.UI.ModalDialog.showModalDialog(options);
    }

Here options are used to set the properties of the dialog. Some more properties are  showClose, allowmaximize etc.

Most imaportant thing is the "dialogReturnValueCallback" this property is used to get the dialog return value when the dialog is closed.

I called this function from code behind and setting the url and title described below:

string url = SPContext.Current.Web.Url + "/SitePages/NewForm.aspx?PID=" + tocid; lnknewitem.NavigateUrl = "javascript: NewItemDialog('" + url + "','New Item');";

IN above code snippet i used a link button and set its navigate url programmatically and called the Dialog open function.

And the DialogCallback function is


 function DialogCallback(dialogResult, returnValue) {

     
          if (returnValue == "1") {
            var meesageId = SP.UI.Notify.addNotification("Loading", false);
            SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
        }

     
    }

If you want to return value from the modal dialog while closing then use the below function on the modal dialog close function


SP.UI.ModalDialog.commonModalDialogClose(dialogResult, returnVal);

ie.

SP.UI.ModalDialog.commonModalDialogClose(1, "1");

Now when dialogcallback function will fire it will check the return value if it is "1" then it will add notification and refresh the parent page.

you can pass dialogResult, returnVal according to your choice and you can get these values on the parent form using DialogCallback function as described above


To simply close the dialog use the below function on client click event of the button

function CloseForm() {

        window.frameElement.cancelPopUp();

        return false;

    }



If you want to open the new custom page in modal dialog and ribbon is displayed on the modal dialog then use this css trick to hide ribbon

 #s4-ribbonrow
    {
        display: none;
    }








No comments:

Post a Comment