SharePoint 2010 Pop-Up Dialogs

SharePoint 2010 makes it incredibly easy to add dialog content to your website. Built-in functionality allows you to retrieve content from anywhere within your site and display it in a modal dialog that appears over the page. This is pretty standard stuff when it comes to modern web technology, but the ease of use is what makes SharePoint dialogs so amazing. In this article, we’ll explore the dialog functionality that comes with the SharePoint 2010 platform, and we’ll discover new ways to customize that content to create a uniquely fresh appearance for your SharePoint portal.

Dialogs in SharePoint 2010

Even if you have chosen not to acknowledge dialogs in SharePoint 2010 just yet, they’re already being used throughout the SharePoint platform for things like pages, lists, and libraries. Every time you create a new page, for instance, you’ll see a dialog that appears on the page. It most likely looks something like this:
SharePoint 2010 Dialog

You’ll see similar dialogs when you create and edit list items, as well as within any document libraries that you have created throughout your site collection. These pop-up forms and views are an integral part of the SharePoint 2010 platform.

How it Works

SharePoint 2010 dialogs are fairly simple. They are basically a JavaScript pop-up dialog that include an iframe to request content from elsewhere on your site. This content can be anything inside SharePoint, even publishing pages or custom application pages that you’ve created yourself.

If you’ve ever created your own master page in SharePoint 2010, you may have been confused when you were prompted with two options when trying to apply your new master page to the site. SharePoint 2010 segregates your master page delegation between site master page and system master page. Basically, your site master page will be applied to pages, lists, and libraries, where the system master page is applied to everything else (settings, permissions, administration, and…dialogs). When you call a pop-up dialog in SharePoint, the content is requested and is displayed using system master page. If you want to start customizing the way your dialogs appear in SharePoint 2010, the first step is to format your custom master page to also work as a system master page.

System Master Pages

The most important thing you’ll want to do in order to optimize your master page for dialog use is to designate which elements of the page will be hidden inside a dialog view. If you open Microsoft’s v4.master file, you may notice that class="s4-notdlg" appears in numerous places throughout the file. The s4-notdlg class name should be applied to anything you want to hide in the dialog view. This means headers, footers, navigation, sidebars, etc. Basically, you’ll want to add this CSS class to everything except for the main content area of your design. When dialogs are displayed, anything with the s4-notdlg CSS class is automatically trimmed out.

Dealing with Style

Your design most likely has a fixed width, or at the very least, a minimum width at which it can display. Dialogs are very small in size, so the width and size formatting that you’ve established for your design won’t necessarily work nicely within the dialog view. Fortunately, any time your master page is used to display dialog content, the html element gets a special CSS class name called ms-dialog. You can use this to override any styles that you’ve set for your design, making it appear correctly both in normal and in dialog views. For example, you might have a div element in your design with an ID of wrapper. In an attached CSS file, you can style this element as it appears both in normal and dialog views:

#wrapper {
  width: 960px;
  margin: auto;
}
html.ms-dialog #wrapper {
  width: 100%;
}

In the above example, the wrapper element will switch to fluid width when displayed inside a SharePoint 2010 dialog. You may have to utilize this technique with things like columns, background colors, and other design elements as they appear inside the dialog.

Harness the Power

Now that your master page is optimized for dialog use, it’s time to start pulling some content from your SharePoint site. Creating a pop-up link is extremely easy. On any page or within a content editor web part, create a link to request a dialog page:

<a href="javascript:OpenPopUpPage('/path/to/content.aspx');">Show Me the Pop-Up!</a>

You can use the OpenPopUpPage function to view any content in dialog mode. There is also a more flexible function called OpenWebPage that allows you to specify things like dialog width and height. Using these function in conjunction with a custom system master page, the limits to what you can display within a dialog in SharePoint are really limitless.

Progressive Enhancement

I’m all about accessibility, and I really don’t like it when someone asks me to put javascript: inside a link element. Users without JavaScript won’t be able to follow the link, and perhaps more importantly, search engines like Google or Bing won’t be able to track the content associations within your site. This can be a huge problem, and as such, I wouldn’t recommend using the above method for dialogs in your SharePoint site.

A better approach involves an exercise in progressive enhancement. Progressive enhancement demands that you create a solution that functions for users with the least functionality enabled within their web browsers. This means creating simple HTML links that point to real web page content. By creating simple links, you can guarantee that everyone (including search engines) can decipher and utilize the links in your site. For other users, such as JavaScript-enabled users, you can enhance their experience with things like pop-up dialogs to display content more quickly.

This is easy enough to do in SharePoint 2010, simply because our custom master page is formatted to display content in both normal and dialog views already. We can easily direct users to the normal view of the content, and override that functionality for script-enabled users of our site.

SharePop

In true progressive enhancement fashion, I have created a simple jQuery function that will turn any normal link in your SharePoint site into a pop-up dialog link, simply by associating a CSS class with that anchor tag. Add this functionality to your own SharePoint 2010 site:

  1. Ensure that the jQuery script library is attached to the master page or page layout on which you will utilize my script
  2. Add the following jQuery function to an attached JavaScript file:
    (function($){
      $.fn.sharePop = function(){
        if(typeof OpenPopUpPage == 'function'){
          return this.each(function(i){
            if($(this).attr('href') != null){
              $(this).click(function(e){
                e.preventDefault();
                OpenPopUpPage($(this).attr('href'));
              });
            }
          });
        }
        else{
          return false;
        }
      };
    })(jQuery);
  3. Add a unique CSS class name to the links you wish to display in dialog view:
    <a href="/path/to/content.aspx" class="dialog-pop">View Content</a>
  4. Run the script (add this to an attached JavaScript file):
    $(document).ready(function(){
    	$('.dialog-pop').sharePop();
    });

My script will override the default functionality of these links, and force them to display inside the SharePoint 2010 dialog view instead. Accessibility prevails, as users and search engines which do not utilize JavaScript are still able to view the content (albeit in the normal view of the design).

Dialog Style

The last thing you’ll want to consider when using SharePoint dialogs is the style of the dialogs, themselves. The dialog HTML gives you numerous CSS classes that allow you to completely customize the appearance of the dialog window, and even the appearance of the shaded area behind the dialog. The following image shows the HTML of the SharePoint dialog, and I’ve also highlighted some important CSS classes you’ll use to change the appearance of the windows.
SharePoint 2010 Dialog HTML

Using these classes, you can create a unique appearance of the dialogs in short order. Here is an example of a “dark” theme dialog:

.ms-dlgOverlay {
  background-color: #333;
}
.ms-dlgContent {
  border: 0;
}
.ms-dlgBorder {
  border: 1px solid #333;
}
.ms-dlgTitle {
  background-color: #333;
}
.ms-dlgTitleText {
  display: block;
  font-weight: bold;
  font-size: 13px;
  padding: 7px;
}

After adding the CSS to an attached CSS style sheet, we get a completely custom dialog appearance, along with custom content that appears within the dialog box, itself.
Styled Dialog with Custom Content

There’s a lot of elements coming together in SharePoint 2010 dialogs, and it can take some practice and determination to get all the pieces fitting together quite right. If you can do it once, you’ll find that these custom dialogs are a simple, fast, and amazing tool to add to any SharePoint 2010 design.

47 comments

  1. Christophe says:

    “When you call a pop-up dialog in SharePoint, the content is requested and is displayed using system master page.”
    Kyle, are you saying that even for pages that are based on a site master page, the template will switch to system master page when displayed as dialog?

  2. Kyle says:

    Hi, Cristophe. Yes, all dialogs in SharePoint 2010 use the system master page. If your site master page isn’t formatted for dialog use, you can probably stick to the v4.master, but you’ll lose any CSS and other customizations that you’ve made to your content within the site master page file.

  3. Christophe says:

    Thanks Kyle. That was my point, and it means that if you want to keep the customizations you need to add them to the page via a Web Part, or apply the customization to both Master pages.

  4. Kyle says:

    You can actually format a single master page to work as both a system and a site master page, which is what I generally do. It just means adding a bit of time to your design process to make sure the master is formatted in both views.

  5. Max says:

    Hi Kyle

    How could I wired a timer to the dialog so it close after 10sec

    Thank you

    Max

    • Kyle says:

      Hi, Max. Not sure how to close the dialog other than clicking on the “X” button in the top-left of the window. I’m sure there’s a JavaScript function you can call to do that, just haven’t used it before. Best of luck.

    • Nerudo says:

      Inside the dialog box you will have to code a setInterval() or setTimeOut() kind of function that then does the incrementation up to your stop time. Once you reach the stop time valu you should then call the [code]SP.UI.ModalDialog.close(dialogResult);[/code] That function will close the opened dialog box for you. Setting the dialogResult parameter is kinder optional but useful if you want to do something with the return result.

      I did a write up on how to use the OOB SP.UI.ModalDialog class on my blog here on the dioffrent ways you could further trigger custom dialogs http://nerudo.mregi.com/post/26065841781/triggering-sharepoint-2010-pop-ups-using-javascript

  6. Piet says:

    Hi Kyle

    I would just like to know one thing :
    We busy creating a new site for our Intranet, the thing is, we have this Values image that displays as our Main Page at this stage, but we want this to appear as a pop-up ( when you enter the Intranet site ) along with a box that they have to agree to the terms and conditions. If they accept, the image have to go away. This needs to pop-up every time they enter the site. Is this possible ?

    • Kyle says:

      Hi, Piet. That is most certainly possible, but it sounds like something that I would opt to use Visual Studio to build rather than JavaScript and UI features of SharePoint.

    • Nerudo says:

      You could use a callback to check if the check-box is checked and close the dialog. In your child or dialog box you need to have this function [code]SP.UI.ModalDialog.RefreshPage(dialogResult);[/code] once someone has checed the checkbox they should trigger that function. And additional function you will then have to call is the
      [code]SP.UI.ModalDialog.close(dialogResult);[/code] That function will close the opened dialog box programatically so to speak.

  7. Thiago Silva says:

    Kyle, I have a public facing SP2010 site using the Publishing features. I have a few page layouts for article pages and in there I use rich html content types. When a user is editing one of those article pages, and they insert a picture, then add a hyperlink to the image via the ribbon, they can set the target URL for the link as well as whether or not to open in new tab. However, what we want them to do is to make the hyperlink open a true popup dialog, instead of a new tab/window.

    I tried setting the URL for the link in the ribbon to the “javascript:OpenPopUpPage(‘mypage.aspx’);”, but when I save and close the page, SP strips out that URL so that the result is an tag with no HREF attribute.

    Is there a way for my users to do this (set the link to a “popup dialog” behavior)? If so, can you detail an approach to this? Thanks!

  8. Thiago Silva says:

    follow up to my previous comment: I am able to use jquery to wire up my elements that get hyperlinks which have target=_blank, so they popup. I still have 2 issues:

    1) every image with hyperlink within my content type on the page, which has the “open in new window” checked will leverage the popup dialog….but there are some cases when I don’t want a dialog, burt rather a true new window (e.g. external links). How do I do that?

    2) Popup modal dialogs are being rendered at the top of the page, so if I am scrolled in the center and click the link, I have to scroll to the top to see the modal dialog. Any way to force the modal dialog to be centered on the current viewport?

    thanks!

    –Thiago

    • Kyle says:

      Hi, Thiago. You are on the right track – jQuery is definitely your best option because SharePoint likes to strip your JavaScript from rich HTML fields.

      The best way to single out only the “pop-up” links that you want to alter using jQuery is to use a specific CSS class, like “image-dialog” or something like that — that way, in your jQuery you can modify only those links that have the CSS class applied to them.

      As far as the scrolling thing goes, you might try adding “position:fixed” to the .ms-dlgContent DIV. This probably has some other ramifications that you’ll have to test and resolve, but that would force the dialog windows to always display in the center of the screen, regardless of how far the user has scrolled.

    • Nerudo says:

      Have you tried putting your code in [CDATA] tags?

  9. Thiago Silva says:

    thanks for the response, Kyle.

    Unfortunately, I can’t single out only popup links, since the images are rendered by the PublishingWebControls.RichImageField. I do wrap that control with a div and my own class, which is I what used to apply the popup code previously mentioned. The problem is that I need to identify when the link should popup our when it should open a new window. The ribbon’s Link options menu only provides behavior to open new window. Ideally, I would add a new checkbox for the user to specify “pop up in dialog”, which would render the tag with a custom class. THen I wouldn’t have to use the parent DIV’s css class.

    Regarding the ms-dlgContent css, I tried that, but it breaks the dialog (although, it does center it). I no longer get the title bar, so I can’t close it.

  10. [...] Un ottimo post da seguire per governare anche le personalizzazioni lato stile è indubbiamente questo: http://kyleschaeffer.com/sharepoint/sharepoint-2010-pop-up-dialogs/ [...]

  11. Matthew says:

    I use this post on every SharePoint implementation I do. Great Article and keep em coming sir.

  12. John Mc says:

    Great share – really helped me out. Thanks!

  13. kee says:

    ive juz started my career nd nw into sharepoint. juz suggest me links wer i can kno how to create a master page in sharepoint 2010 without the help of sharepoint designer.

  14. Great, html.ms-dialog was just the thing I was looking for to hide the header and navigation of my custom masterpage.

    Thanks
    Andreas

    • Nadezhda says:

      Yeah, I have serious obonetijcs to a toolbar across the whole window telling me that it has blocked a popup who cares, the I was just fine.Next we’ll be having a popup window telling us that we’ve blocked a banner ad!Also, doesn’t every app have a find dialog instead of yeat another toolbar?! Soon Firefox won’t have enough room for the web pages with all these toolbars.This kind of cr*p should be left as an Extension, or have the option to kill it.

  15. ixiel says:

    Great! and Thank you for the tips.. after hours of digging at last i found your post.

  16. Ameet Kumar says:

    hey guys… could any one help me out here?? i want a pop up to be displayed wen the page is loaded. The pop up will carry a static tracker. I m stuck wid it big tym…pl reply….

    • Nerudo says:

      just trigger the function on load using jQuery like so

      [code]
      $(function(){

      OpenPopUpPage('yourPage.aspx');

      });
      [/code]

      Not knowing what you tracker needs to do you could implement a call back function that then passes the tracker value page to the parent page.

  17. Ameet kumat says:

    hey guys… could any one help me out here with sharepoint?? i want a pop up to be displayed wen the webpage is loaded. The pop up will carry a static message. I m stuck wid it big tym…pl reply….

    thnx..
    Ameet

  18. Chris Dill says:

    Great article, thanks a bunch. I have a question for you. I have a custom intranet site which has a subsite which I have heavily modified. In order to get the content I wanted to work working correctly, I had to apply a background image and styling to

    body #MSO_ContentTable{
    background-color: transparent;
    border: none;
    margin: 10px 0px 0px 10px;
    padding: 5px 0px 0px 200px;
    overflow: hidden;
    height:auto;
    min-height:400px;
    clear:both;
    position:relative;
    background-image:url('../../images/CompanyLetterhead.png');
    background-repeat:no-repeat;
    }

    This then themes all lists and libraries, which I then have further customized using different views and hidden features.

    The problem is that this is also the css used for the dialog boxes. I tried changing the master page to a custom CSS id to no avail.

    I then tried your trick

    .ms-dialog #MSO_ContentTable {
    background-color: transparent;
    border: none;
    margin: 10px 0px 0px 10px;
    padding: 5px 0px 0px 200px;
    overflow: hidden;
    height:auto;
    min-height:400px;
    clear:both;
    position:relative;

    }

    And this did not work either. Any tops on getting this working correctly?

  19. Chris Dill says:

    Never mind, I figured it out. This is what I used, though I am sure there are some unused attributes…

    html.ms-dialog #ctl00_MSO_ContentDiv {
    margin:0;
    padding:0;
    width:auto;
    overflow:visible;
    }
    html.ms-dialog #MSO_ContentTable {
    margin:0;
    padding:0;
    background-color: white;
    background-image:none;
    border: none;
    width:100%;
    position:relative;
    overflow:visible;
    }

  20. Tom says:

    So, I need to figure out where the initial width and height are coming from. I have ad user requests that they want the dialog box opened to the max on popup. Is this possible?

  21. Nicolás Lasso says:

    Hey!

    I’m working on an intranet site and have both system and site master page. The problem I’m having is that the Pop-up box doesn’t appear on the center of the page but on the top and I have to scroll up to see them. Do you happen to know how to fix this? Thnx!!

    Nico

  22. Tom says:

    Thanks Kyle. Really helpful.

  23. infissi says:

    I have fun with, result in I discovered exactly what I used to be taking a look for. You’ve ended my four day lengthy hunt! God Bless you man. Have a nice day. Bye

  24. Praveen says:

    Great thanks for writing this post !!! I was struggling with this popup dialog display from so many days ..thinking of writing a java script function. but this is the most cleaner and efficient way of doing it ..thanks again

  25. Chris says:

    Kyle, you’re the man. This is a great resource.

    Thanks for all of your efforts.

  26. Jithu says:

    Hi Kyle, Thanks a lot for your code, however would like to request your little help,
    Here I have created a web part where I have a gridview with edit button for editing rows. On edit button click I will use openpopup page to edit the items with the row ID.

    I have two buttons,

    1. update (C# code is updating the edited values correctly).
    However if no exception I would like to close the popup page and refresh the parent page accordingly, How can I do it?

    2. Cancel (Need To close the popup page)
    Did try with, OnClientClick=”javascript:this.close();” , No luck in getting this…

    I would request ur kind help on these two problems…

    Thanks ,
    Jithu

  27. Rohit says:

    I am custumise the master page. I am using sharepoint default popup to take input but popup will not loaded at the center of the screen. but the default sharepoint popup will opens in center position of the screen please help me for this problem.

  28. Uwe says:

    This is fantastic! The first hit when querying Google for “sharepoint popups” and immediately reading your well-documented overview with lots of details. Thanks!

  29. [...] http://kyleschaeffer.com/sharepoint/sharepoint-2010-pop-up-dialogs/ Share this:TwitterFacebookLike this:LikeBe the first to like this post. This entry was posted in Uncategorized by rickycheng261448928. Bookmark the permalink. [...]

  30. Lindsy says:

    Thank you so much for sharing the “class=”s4-notdlg”" I was having the toughest time trying to figure out how to hide the header that I created. Worked like magic!

  31. Daniel says:

    This was great, thanks! Do you think it would be possible to extend the jQuery code so that when the dialog box is closed, the parent page refreshes? This seems to be the typical behaviour when you click on a link such as Add New Item on a list and makes it clear that the item has successfully been added. I’d be really appreciative of any help or suggestions. Thanks again!

  32. Kash says:

    Thank you very much. Exactly what I was looking for.

  33. Basel says:

    Hi Kyle,
    recently we have finished a branding session on our portal SharePoint 2010, all goes well, till someone raise an issue about the “Respond to this Survey” button that is not working! “dialog was not showing with the survey form?”,
    so after checking I found that it was hiding the complete page for seconds and then re-appear without showing the dialog of the survey form.
    Only after changing back the system master page to “nightanday.master”, of course I lost my design , the button works properly, and the dialog now is showing the survey form!

    Is there something i missed in my custom master page, that cause this symptoms!

  34. Sri says:

    Hi Kyle,

    I have customer list. I opened that list in InfoPath 2010 and did some customization. Is it possible to add SharePoint 2010 Popup Dialogs when I add new item, save and close this customer list.

    Thank you

    Sri

  35. avinash says:

    Nice article.

    I have consolidated all popup related things and made an article:-
    http://sharepoint.infoyen.com/2012/10/24/sharepoint-2010-popup-dialogs/

    Thanks
    Avinash

    • Rick says:

      Thank you Kyle, Avinash and ALL, your contributed GREAT WORK makes my life much much easier as a SharePoint developer. These valuable resources are exactly what I am looking for for my custom solution.