Monday, July 21, 2008

InfoPath Rules vs Code

When writing custom code behind for an InfoPath form you should keep in mind that when used on a button with other rules that the rules will run before the code. This means that if one of your rules "Closes the form" that your code will have to do this instead, otherwise the rule will close the form and your code will never run.

There is no way to switch the OoE or Order of Execution within InfoPath. Here is a sample to close the form in code in a js script code behind. There is also no need to sign the form unless the code interacts with the SharePoint OM.

Application.XDocuments.Close(0); //will close form but keep InfoPath open

Application.Quit(bool) //will exit InfoPath and if true force an exit, if false will prompt user if they want to save the form

I used true because in a rule I submit the form so the code can go ahead and quit InfoPath.

7 comments:

Anonymous said...

This is great info to know.

Anonymous said...

Exactly what I needed. Thanks!

Anonymous said...

If the Quit method is used in a form that is not fully trusted, the method will return a "permission denied" error.
Reference: Application.Quit Method

Shan said...

All of these apply to Filler Forms. Is there any method to close the browser form?

@SPJeff said...

How can this be done with Browser Forms? Thanks!

Natalia said...

Thanks for this wonderful information. I will do keep in mind about this useful point and make sure to follow the order of Execution while writing the code.
infopath

Anonymous said...

There are exceptions though.
When you have FormLoading Rules and FormLoadEvent in CodeBehind the order seems to be the opposite when opening the form in a browser.

When I debug in infopath the rules are executed before code - when I publish to sharepoint the code is executed before the rules.

nano