Automatically Refreshing Microsoft Dynamics CRM 4.0 Forms from Custom Pages

Roshan Mehta, 02 September 2010

I recently created a custom ASPX page for the Opportunity entity in Microsoft Dynamics CRM 4.0. The logic behind this custom page is to allow a Dynamics CRM user to add products to the Opportunity, and click a button which would create the Opportunity Product records, and then close the Opportunity with a status of “won”. However, when the “OK” button was clicked and the page was closed, there was no immediate change to the Opportunity record, unless the user manually hit F5 to refresh the browser.

It is very important to make sure that any custom pages that are created for Dynamics CRM mimic the out-of-the-box functionality as close as possible. Leaving the page alone would create great confusion and frustration for all users as they would think that the operation had failed, since the Opportunity will still appear to have a status of “Open”. To automatically refresh the parent (Opportunity) form, you’ll need to write something similar to the following C# code:

        protected void okbutton_Click(object sender, EventArgs e)

        {

            // add your custom processing here

 

            ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript", "window.close()");

            ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript", "window.opener.location.reload()");

        }

Here is an example of this process in use.

 

This will greatly improve the user experience by reducing any confusion with the current status of the Opportunity. The Opportunity form will reload when the “OK” button is clicked, and display each field in a read-only format.

This is a very simple piece of code which you can use for any page that results in a status change for any entity record.