Saturday, October 8, 2011

Close SharePoint 2010 Dialog through code

In SharePoint 2010, the new feature is added is the dialog framework. With the help of this, user stays on the same page and able to get the information without go away from the current page. So, there are ways that we can close the dialog from the code in different ways. Below are the mainly used implementations in javascript and code behind.
Javascript:
<script type='text/javascript'>window.frameElement.commitPopup()</script>

The window.frameElement is the node which holds the current dialog window. commitPopup() method which commits the popup and closes the dialog from the page window. So, by simply calling the above method, the dialog window will close automatically.

Code-Behind:
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();
}

The dialog framework recognizes an additional parameter in the request named "IsDlg=1", which says open the page in dialog box. So, in code-behind simply check for query string parameter and if it exists then call the javascript method mentioned above by writing it to browser. So, when it executed the dialog will be closed.

This is a very nice tip which helps in some custom implementations. Hope it helps.

3 comments:

  1. Nice tip - thanks

    ReplyDelete
  2. Thanks for the tips, I tell you the problem I have with some SharePoint lists. The ribbon disappears when entering text making it impossible to save the item. Then I go to advanced settings and turn off the dialog so I can just display the post in a full window. However even when that is set to no the dialog still appears. What can i do?

    ReplyDelete
    Replies
    1. I had this same issue. I know this response is a bit late, and perhaps you already have the answer, but maybe someone else will benefit from my resonse. The System Master page dictates control parameters for the modal dialog boxes. You need to open up your system master page in SP Designer, go to the line of code that is for the element s4-ribbonrow (this is the editing ribbon), and remove the attribute "class=s4-notdlg". This "notdlg" class is telling you master page to "not display in dialog boxes".

      Delete