Saturday 18 June 2011

How to close the sharepoint modal dialog and refresh the parent page.

Recently, I stumbled upon a situation where I had Calendar list and I had provided custom save and cancel button.

In List setting I had kept "Launch forms in a dialog" option to "No" so that my NewForm/EditForm/DispForm can open on same page instead of a dialog window.

But in case of Calendar View Edit and Display form always opens in Dialog.So after updating the record we explicitly need to close the dialog and redirect to any view.

At server side I tried below code.

Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
Context.Response.Flush();
Context.Response.End();

But It didn't worke!!!! So I tried below approach and it worked.

Since I had to close the window only user is editing form Calendar view.So wherever you open a dialog a querystring is passed with name "IsDlg" and value ="1". So If ""IsDlg" is present in the URL redirect the user to your custom page from server side, and use below javascript to close the dialog.

Server Side Code


if (Request.QueryString["IsDlg"] != null)
{
dlgID = Request.QueryString["IsDlg"].ToString();
}
if (!string.IsNullOrEmpty(dlgID))
{
Response.Redirect(SPContext.Current.Web.Url + "/Default.aspx?Close=1");
}


Write below code in Default.aspx or your custom defualt page.

var closeFlag =location.search.substring(1).split("Close=");
if(closeFlag !="")
{
var flag = closeFlag[1].split("&");
if(flag=="1")
{
Close();
}
}

function Close()
{
// Will close the window and update the view.
window.frameElement.commitPopup();
}
</script>

No comments:

Query List/Document Library in Specific Folder

To query SharePoint List or Document Library in specific Folder “ FolderServerRelativeUrl ” as part of the CAML Query Code Snippet ...