How to Open/Close Modal Dialogs in SharePoint 2010

SharePoint 2010 comes with a new modal dialogs feature that allows you to Open Modal windows from you application pages or ribbons. In this post i will highlight on how to open and close Modal Dialogs in SharePoint 2010.

If you have an Application page and you want to open up a modal dialog this is what you want to write in JavaScript using COM (Client Object Model) :

In your page, Add the following button to open modal dialog:

input onclick=”openPopup();” title=”Open Modal” type=”button” value=”Open Modal Dialog”

On you Page Head add the following JavaScript function implementation “openPopup”:

function openPopup() {
var options = {
url: ‘/_layouts/MYPROJECT/DialogPage.aspx’,
title: ‘Modal Dialog’,
allowMaximize: false,
showClose: true,
width: 600,
height: 500
}
// Open Modal Dialog
SP.UI.ModalDialog.showModalDialog(options);
}

DialogPage is an application Page in your solution as specified above, Now try to deploy you solution and click on your button, your application page is showing as modal dialog… THIS IS AWESOME !!

To close you modal window, this is the line you need to add on you cancel or close button on your page:

SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.cancel, “Cancelled”);









Hope this helps, To get the full documentation for ModalDialog using Client Object model, here is the KB url:



Regards,
Mostafa Arafa
twitter.com/mostafaelzoghbi

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>