Working with SharePoint 2010 modal dialogs

A new feature of SharePoint 2010 is the modal dialog which resembles the classic LightBox behavior. This allows us to add and edit items, manage meta data and perform administrative tasks within the context of a page without leaving the page.

Fig 1: SharePoint 2010 modal dialog 

Fig. 1: SharePoint 2010 modal dialog

The code uses the client library SP.UI.ModalDialog and the method showModalDialog which can be called directly from the page as:

   1: function test() { 











   2:     //debugger; 











   3:     SP.UI.ModalDialog.showModalDialog({ 











   4:         url: "/Lists/Announcements/NewForm.aspx", 











   5:         title: "Add item", 











   6:         allowMaximize: true, 











   7:         showClose: true, 











   8:         width: 800, 











   9:         height: 600, 











  10:         dialogReturnValueCallback: silentCallback 











  11:     }); 











  12: }; 











  13:  











  14: function silentCallback(dialogResult, returnValue) { 











  15: } 











  16:  











  17: function refreshCallback(dialogResult, returnValue) { 











  18:     SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK); 











  19: }





















The ModalDialog takes an option structure of SP.UI.DialogOptions. At the moment there is no MSDN documentation around this, but keep looking at the documentation at http://msdn.microsoft.com/en-us/library/ff408909(v=office.14).aspx.









The code opens a new modal lightbox dialog in the “dialog” display mode. The SharePoint page that we want to load will have the usual master page. This will not be needed in a modal dialog, so the scripts appends the parameter IsDlg=1 to the URL.









Regular “NewForm.aspx





image









Using “IsDlg=1









image

10 comments:

Chris G said...

Hey!

Tobias ...

Nice site. I have a quick question on modal dialogs:

I have a SharePoint list. As you know, all list items have a default context menu that allow the user to choose certain options such as Edit, Delete, etc. When you click on the Edit option - it opens up with the new SharePoint 'modal dialog'. I REALLY like the whole modal dialog idea - it focuses the user in on the task at hand, and doesn't allow them to click anything other than the items within the open 'window'.

However ... using SharePoint Designer, I have added a few of my own custom items in the context menu. They work fine ... but they DON'T open up in the same 'modal dialog' - they simply open on my SharePoint screen.

There doesn't seem to be a simple 'click here' to put custom context menu items within a modal dialog box. I don't mind limited coding - just nothing too elaborate. Is there a 'lump of code' that I can add or incorporate into my custom context options that would place them within a modal dialog box? Is there any site you could point me to that would help me with this?

Thanks!

Tobias Lekman said...

Hi Chris,

Sure, this can easily be done.

Instead of a URL, enter the JavaScript directly as, for example:

javascript:SP.UI.ModalDialog.showModalDialog({ url: "/Lists/Announcements/NewForm.aspx", title: "Add item" }); return false;

I added a longer explaination here.

Good luck.

//Tobias

Anonymous said...

Here a good link that provide details about the Model Dialog of Share Point 2010

http://www.a2zmenu.com/SharePoint/SharePoint%202010%20Model%20Dialog.aspx

Anonymous said...

Hi,

I am creating a webpart and have the following code that opens an announcement item but I can not figure out how to modify it to make it open in a dialog modal. Any thoughts?

read more

Thanks,
Rene

Tobias Lekman said...

Hi Rene,

The link you provided does not go anywhere. Can you repost the code that you point to?

Mor Shemesh said...

Hi,
I've been working with modal dialogues and came across an annoying occurrence:
when I try opening a dialogue inside another dialogue (I open a display dialogue from which I open a new dialogue of a second list) when I submit the second dialogue it refreshes with no data and the first one also refreshes, and I can't close the second dialogue...

Tobias Lekman said...

Hi,

I have not seen this before and don't think it's supported. Is the issue with the redirect or that the wrong window is closed?

Mor Shemesh said...

n.m. it was a bug eventually of opening two windows in the same time (one behind another).

And it does work really good :)

2010Ben said...

Hi, Nice article!

I'm having a problem where the modal dialog is opening and displaying my custom application page just fine with all the masterpage sections cut off, however when a postback is done within the window and the page refreshes within the window... all the masterpage sections are back. Any idea how to stop this happening?

Tobias Lekman said...

Hi Ben,

Back from holiday now, saw your post.

No, that shouldn't happen unless you do a redirect using Response.Redirect and do not include the querystring parameter IsDlg in the redirect.

Post a Comment

Feel free to add your comment to this post. All comments are moderated and may not appear immediately within the page.