SharePoint Modal Dialogs

SharePoint 2010 finally provided a way out of the horrible page refresh and post backs of SharePoint 2007 when it comes to viewing, editing, and adding items to libraries and list in the form of wonderful JavaScript modal dialogs.

What is even more awesome is that the even provided a way to make it optional for us. Why would would you want to provide full page navigation to these forms? The most common reason is if you have a complex custom form with a lot of fields or data, the full screen version provides benefit to the user, in limiting the amount of required scrolling they may need to do. Also, a concern is for accessibility issues where a screen reader may not detect the modal causing your user to miss out on there details they were hoping to consume.

Turn Off From GUI
To turn off the Modal Dialog from the GUI, the web browser is simple enough.

  1. Navigate to the list. From the List Tools of the Ribbon click List –> List Settings.
    image
  2. This will bring up the List Settings page. From here under General Settings  click Advanced settings.
    image
  3. You will then see the Advance Settings screen. Scroll to the bottom and you will find a row titled "Dialogs" with a radio buttons for Yes and No.
    As the description describes this turns the modal dialogs on and off.
    Yes being display as modals, and No being display as a page.
    image
  4. Click OK and the setting will take effect. Simple once you know where to look.

Turn Off via Object Model
Sometimes there is a need to set the Dialog value via code in the object model. There does exist a property on an SPList object called "NavigateForFormsPages" which controls this setting. As with many other SharePoint object properties the name does not always describe the related purpose.

"NavigateForFormsPages" is a boolean value where
True causes the forms to render as a full page, post back and all. While False, indicates that form rendering should be in the modal dialog. As with any list property settings don’t forget to call SPList.Update() after setting the value.

Here is some sample code of how to set the NavigateForFormsPages value from a Web scoped feature receiver:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    using (SPWeb web = (SPWeb)properties.Feature.Parent)
    {
        web.AllowUnsafeUpdates = true;
        SPList newList = web.Lists["CourseDirectory"];
        if (newList != null)
        {
            //True - full page rendering
            //False - modal dialog rendering.
            newList.NavigateForFormsPages = true; 
            newList.Update();
        }
    }
}

See http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splist.navigateforformspages.aspx on MSDN for the super technical details regarding this list property.

Modal Dialogs and Custom Forms

You’ve written a super sweet custom application page for your content type, and am ready to set it free in your environment. Turns out during the stage testing (you do have a stage server right?) you custom form doesn’t actually close the modal dialog it resides in. Instead the modal dialog opens the redirect link in the current dialog. Possibly your close event code looks something like the following:

protected void btnExit_OnClick(object sender, EventArgs e)
{
    if (Request.QueryString["Source"] != null)
        Response.Redirect(Request.QueryString["Source"], true);
    else
        Response.Redirect(SPContext.Current.List.DefaultViewUrl, true);
}

You might see the following strange behavior where you modal never actually closes.

First, we open our item in the custom form, and see

image

After clicking CLOSE button which has the above custom code we get this:

image

Since our current context is still the frame set of the modal dialog our supposed closing just loads the code into the current window frame. Luckily,  with a little code and checking the current HTTP context we can close the modal or redirect if modal is turned off. Our new close event code would be this:

protected void btnExit_OnClick(object sender, EventArgs e)
{
    HttpContext context = HttpContext.Current;
    if (HttpContext.Current.Request.QueryString["IsDlg"] != null)
    {
        context.Response.Write("<script type=text/javascript>     window.frameElement.commitPopup();</script>");
        context.Response.Flush();
        context.Response.End();
    }
    else if (Request.QueryString["Source"] != null)
        Response.Redirect(Request.QueryString["Source"], true);
    else
        Response.Redirect(SPContext.Current.List.DefaultViewUrl, true);
}

Now, the following will happen:

image

And after close, a view of the All Items page of our list:

image

Now you have the know how to take advantage of the new modal dialog feature of SharePoint 2010.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s