17 June 2010

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

17 comments:

  1. Chris G23/8/10

    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!

    ReplyDelete
  2. 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

    ReplyDelete
  3. Anonymous28/8/10

    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

    ReplyDelete
  4. Anonymous20/1/11

    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

    ReplyDelete
  5. Hi Rene,

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

    ReplyDelete
  6. 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...

    ReplyDelete
  7. 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?

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

    And it does work really good :)

    ReplyDelete
  9. 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?

    ReplyDelete
  10. 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.

    ReplyDelete
  11. Thanks for sharing, I will bookmark and be back again

    Sharepoint Development | Sharepoint Developers

    ReplyDelete
  12. Hi Tobias,

    Great post, but need to ask you if you have ever come across this "issue".

    I have a list which has a lookup field to another list and allows multiple select and added a custom "NewForm.aspx", on the new form i added a "Add New Supplier" button next to the "Select Suppliers" field.

    The modal opens and I add the new supplier, when I close the add new supplier dialog the list of suppliers is not refreshed to show the newly added supplier. However, if I dont allow multi select (in which case its a dropdown), then try again, the supplier list is correctly refreshed and shows the newly added supplier.

    So difference is Lookup as a 1) Multi select 2) Single select.

    Multi-select doesnt refresh :-(

    Can you help?

    I posted about it here too : http://social.technet.microsoft.com/Forums/en-us/sharepoint2010general/thread/a38cbc86-96c9-4bd3-869f-16207533c0a1

    ReplyDelete
  13. Just copy and paste the below content in your files. That's it.

    Please refer to know get clear idea:
    http://sharepointquicksolutions.blogspot.in/2012/06/sharepoint-workflows-interview.html

    html{
    background-color:#55A0A7;
    overflow/**/:auto;
    }
    body.v4master {
    width:1024px;
    margin:0px auto;
    overflow:visible;
    background-color: transparent !important;
    }
    .ms-dialog body.v4master {
    height: 100%;
    overflow: hidden;
    width: 100%;
    }
    body #s4-workspace {
    left: 0;
    overflow: auto !important;
    overflow/**/: visible !important;
    position: relative;
    width: 100% !important;
    display:table;
    border-spacing:0px;
    }
    .ms-dialog body #s4-workspace{
    display:block !important;
    overflow/**/: auto !important;
    }

    Please refer to know get clear idea:

    http://sharepointquicksolutions.blogspot.in/2012/06/sharepoint-workflows-interview.html

    ReplyDelete
  14. Nice pot.....I would like to know is it possible to open a SP Modaldialog box over other SP ModalDialog box. For example: from View item modal dialog, possibility to open another modal dialog box.

    Regards,
    Rajan

    ReplyDelete
  15. Anonymous23/11/12

    I tried the above technique with a custom displayform but i get nothing. I appended ItemID after the aspx string but still i get no display at all. Is it possible to get a Modal Display Form or is it just for New and Edit forms? thanks.

    ReplyDelete
  16. Hello.

    Thanks for the post. However, when I do this, my modal dialog does not close automatically and I've got "Access is denied" javascript error.

    Second problem I'm having is that my scrollbar is missing on the form

    ReplyDelete
    Replies
    1. Hi,

      Sorry for the late reply, I am away on business travels at the moment. What code and browser are you using?

      Delete

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

Tobias Lekman
Search this blog