Refresh Opening Grid View from a Form in Dynamics CRM 2011

Paul Nieuwelaar, 22 April 2012

I was working on a project recently where we needed to use a custom Silverlight screen to deactivate a record, and then close the open form. This meant you were taken back to the ‘Open Records’ view where you first opened the record from. After deactivating the record using Silverlight, we close the open form using JavaScript:

    //save and close the form
    Xrm.Page.data.entity.save('saveandclose');
    Xrm.Page.ui.close(); 

However when the form is saved and closed using JavaScript, the view that the record was opened from is not refreshed. Instead the now ‘Inactive’ record is still showing in the ‘Active’ (default) view. If we refresh the view, it disappears as expected; however it needs to disappear straight away after deactivating/closing the record.

Before we close the form, we can add a few more lines of JavaScript to refresh the View. Simply add the following code into your JavaScript event, which will force a refresh on the parent/opening view. The try/catch is added to prevent any errors if the record is opened from somewhere other than a view, in which case nothing needs to be refreshed anyway.

//if possible, refresh the view
try {
    window.parent.opener.document.getElementById("crmGrid").control.refresh();
}
catch (e) { } 

When we need to save and close our form now, we can also use this code to refresh the parent view. Once the record has been deactivated by Silverlight, and the form has been closed, you will instantly see the record disappearing from the Active view.

This code can also be used if you are simply using JavaScript to save and close a form, but you want the data in the view to be updated with your new information.