Friday, December 6, 2013

Close modal dialog after page validation passes

Requirement:
To open the default Create Group (newgrp.aspx) page as a dialog box from our custom page on a button click. Close the modal dialog box after page submits.
We created a copy of newgrp.aspx page and used that page to achieve this.

Issues we had:
1. Default “create group” page redirects to the people.aspx page after successful page submission.
2. Closing the dialog on Window.unload worked fine, but the dialog got closed even when the validation fails as validation refresh the page.

Solution:
Create an HTTPModule to check the current page and previous page details though HTTP events. Below is the HttpModule code we used to achieve this.

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DSPModule
{
    public class DynamicModule : IHttpModule
    {

        string strParentPageURL = string.Empty;
        string strCurrentPageURL = string.Empty;

        public DynamicModule()
        {
        }

        public String ModuleName
        {
            get { return "DSPDynamicModule"; }
        }

        // In the Init function, register for HttpApplication events by adding your handlers.
        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        }
        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpContext currentContext = HttpContext.Current;
            Page page = currentContext.CurrentHandler as Page;
            if (page != null)
            {
                page.LoadComplete += new EventHandler(page_LoadComplete);
            }
        }
        void page_LoadComplete(object sender, EventArgs e)
        {        
             HttpContext currentContext = HttpContext.Current;
             if(currentContext.Request.UrlReferrer != null)
                strParentPageURL = currentContext.Request.UrlReferrer.ToString().Trim().ToUpper();

             strCurrentPageURL = currentContext.Request.Url.AbsolutePath.ToString().Trim().ToUpper();
             if (strParentPageURL.Contains("/_LAYOUTS/DSPHANDLER/DSPNEWGRP.ASPX") && strCurrentPageURL.Contains("/_LAYOUTS/PEOPLE.ASPX") && strParentPageURL.Contains("?ISDLG=1"))
             {
                 HttpContext.Current.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
                 HttpContext.Current.Response.Flush();
                 HttpContext.Current.Response.End();
             }
        }
        public void Dispose()
        {

        }
    }
}

Make sure that you add the following entry in the web.config file of the web application at path "configuration/system.webServer/modules"
<add name="DSPDynamicModule" type=" DSPModule.DynamicModule, DSPModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=fa63464975c1a198" />

Feel free to discuss if you have better way of doing this.


No comments:

Post a Comment