Cancel dynamic application close and handling changes in webforms

Aug 15, 2012 at 1:44 PM

Hi,

This might be considered as two separate questions, but they are somewhat related.

Scenario:
- A dynamic web application (CRM Web app) is openened in a session.
- User makes some changes in web app, but does not save [in CRM Web app]
- User closes the dynamic application by clicking the closebutton in wpf panel tab

Result:
1) Dynamic Application closes immediately
2) An Internet Explorer Dialog box appears with message asking user "Are you sure you want to leave this page?" with option to "Leave this page" or "Stay on this page".

...and of course, selecting "Stay on this page" does not have any effect.

Questions are:

1) How can I prevent the message (just accept the fact that changes are lost)
AND/OR
2) Only close the dynamic application if the user selects "Leave this page" (given that page is changed)

At first, I was trying to figure out how to prevent the dynamic application from closing at all. And actually - I couldn't find a way to do such task in code. The UII core only prevents dynamic applications from closing if the app is involved in a workflow - not possbile to cancel it after user has clicked the close button.

Or am I wrong here - is it possible to cancel dynamic application close?

Thanks in advance,
Johan

Aug 17, 2012 at 12:03 AM

Your getting that message from CRM because some bit of data changed on the form,

To get around it, on the close, use the Xrm.Page Save command to save the record on close.   That will save the record in CRM and then close the window.

Mattb.

Aug 17, 2012 at 8:54 AM

Hi Matt,

Ok, this raises a few more questions:

1) Where and/or when do you think I should inject the [Javascript] code that calls the save function? I found an article where you do this in a Webadapter (http://blogs.msdn.com/b/mbarbour/archive/2010/11/22/how-to-override-the-crm-4-0-popup-alert-in-cca-when-redirecting-a-popup.aspx) but I don't know if this is relevant to this particular problem. I guess that another way would be to deploy the JS-code as a web-resource in CRM, but I think this could be a bit risky since we only wan't this behaviour when using the CRM Web forms in the CCA context. If you have any samples where the Xrm.Page* library is used in a CCA-context this would be much appreciated.

2) In my scenario, I would like to call the Save function due to a Dynamic Application Close (and only if the user has done unsaved changes + only if the user really wants to save the changes). There are several things that could go "wrong" here (validation errors, user want's to do more edit etc.)  where it is desired that the web app remains open. Is this even possible? I.e. is it possible to cancel the termination of a dynamic application when user has clicked the close button?

Thanks,
Johan

 

Sep 11, 2012 at 12:32 PM

Hi JohanH,

iam facing a similar Problem - have you found a solution to trigger XRM.Page functions out of a CCA-Context?

 

thanks in advance

ndi

Sep 11, 2012 at 1:17 PM

Hi ndi,

Unfortunately no - havent found a way to solve it.

Best regards,
Johan 

Sep 12, 2012 at 4:00 PM

I will try it get an example up on my blog in the next day or so that shows how to call into Xrm.Page from a web application adapter.

Mattb-msft.

Sep 12, 2012 at 8:56 PM

That would be awesome!

Thanks for your support!

Sep 25, 2012 at 12:44 PM

Hi Matt,

Did you get a chance to look at it.. ?? I'm still strugling to get it done !!! When I tried the following code from WebAdapter, It give me some exception (when I check from the IE tools, I found its not getting correct Xrm)...

((mshtml.HTMLDocumentClass)this.Browser.Document).parentWindow.execScript("Xrm.Page.ui.close();", "JavaScript")

Any other ways of doing it ???

Thanks,

Srikanth

Oct 1, 2012 at 9:04 PM

So the way that I was able to do something similiar was to create a JSCript library in CRM and attach it to my form.  From there in my WebApplication adapter I could then access that libraries function from within CRM.  Since the library and form itself is rendered in an iframe (not the ribbon or left-nav, just the main form), I had to grab the reference to the IFrame in order to actually call my function. 

 mshtml.HTMLDocument _doc = (mshtml.HTMLDocument)Browser.WebBrowser.Document;
        IHTMLWindow2 _win2 = (IHTMLWindow2)_doc.frames.item(0);
        mshtml.HTMLDocument _contentIFRAME = (mshtml.HTMLDocument)_win2.document;
        object _result = _win2.execScript("CCA_RecordCallNotes('" + data + "');", "JScript");}

 This has worked really well for me when interacting with CRM forms, especially with some of the more complex types likes lookups etc.

Jeremy

Apr 21, 2013 at 7:16 PM
JohanH wrote:
Hi, This might be considered as two separate questions, but they are somewhat related. Scenario: - A dynamic web application (CRM Web app) is openened in a session. - User makes some changes in web app, but does not save [in CRM Web app] - User closes the dynamic application by clicking the closebutton in wpf panel tab Result: 1) Dynamic Application closes immediately 2) An Internet Explorer Dialog box appears with message asking user "Are you sure you want to leave this page?" with option to "Leave this page" or "Stay on this page". ...and of course, selecting "Stay on this page" does not have any effect. Questions are: 1) How can I prevent the message (just accept the fact that changes are lost) AND/OR 2) Only close the dynamic application if the user selects "Leave this page" (given that page is changed) At first, I was trying to figure out how to prevent the dynamic application from closing at all. And actually - I couldn't find a way to do such task in code. The UII core only prevents dynamic applications from closing if the app is involved in a workflow - not possbile to cancel it after user has clicked the close button. Or am I wrong here - is it possible to cancel dynamic application close? Thanks in advance, Johan
Hi Johan,

Are you able to resolve this issue? If yes, Can you share your knowledge on this please..

Thank you

Srikanth
Jun 12, 2013 at 11:43 PM
Any good solution to this issue? I just want to completely avoid the message, discard the changes. Thanks!
Jun 13, 2013 at 9:47 PM
Ok, I have it working with CRM 2011 rollup 11 + CCA R2 combining the webadapter from Matt's blog post, the technique for finding the window in jwinchell forum post above and the javascript on this blog: http://mahenderpal.wordpress.com/2011/07/01/close-ms-crm-2011-form-using-js/