Search This Blog

Thursday, May 12, 2016

Open SharePoint 2010 modal dialog and refresh the parent page from custom form

Objective:

1) Open a SharePoint 2010 popup and refresh the parent page when one closes it.
2) Build a custom form that at “submit event” closes the popup and refreshes the parent page.
Solution:
-          Open a Popup using “Javascript” and “SP.UI.$create_DialogOptions();”
-          Add the method “CloseCallback” using the “dialogReturnValueCallback” property
-          Add some JavaScript code inside the CloseCallback to reload the parent page, like the following
 example:

<a href="javascript:openModalDialog('form.aspx');">Open My Custom Form</a>

<SharePoint:ScriptLink ID="ScriptLink1" Name="sp.js" runat="server" OnDemand="true" Localizable="false"
 />

<script type="text/ecmascript">

    var options;
    function openModalDialog() {
        options = SP.UI.$create_DialogOptions();
        options.width = 300;
        options.height = 100;
        options.url = SP.Utilities.Utility.getLayoutsPageUrl('customdialog.htm' );
        options.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);
        SP.UI.ModalDialog.showModalDialog(options);

    }

    function CloseCallback(result, target) {
        location.reload(true);
    }

</script>

Now when you close the popup, the parent page is refreshed.
If one is working with a Custom Form and wants to close the popup and refresh the parent after
 the Submit,
he could have two (or more) choices:
1) If you have 1 post back only, after the submit, the you can add in the page load the following:
protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                string script = "<script language='javascript'>parent.CloseCallback();</script>";
                if (!this.Page.ClientScript.IsClientScriptBlockRegistered("rKey"))
                    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "rKey", script);
            }
        }

          2) If your custom form handles more than 1 post back, you can add the following code at one 
specific submit click event:
Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup()
;</script>");
Context.Response.Flush();
Context.Response.End();
The last script is suggested at the following link: http://social.msdn.microsoft.com/Forums/en-

No comments:

Post a Comment