Work with offline solutions using the InfoPath object model
The InfoPath 2003-compatible object model provides the MachineOnlineState property of the Application object which enables your form code to check whether the user's computer is connected to the network. Your form code can perform different actions depending on the state of the connection.
Using the MachineOnlineState property
The following example shows how you can add logic to your form code that determines how to submit a form based on whether the user's computer is online or offline.
This example assumes that you have created a form for submitting a sales report that contains a field named "period" that specifies the month and year covered in the report. It also assumes that you have already defined a data connection and the logic for submitting the report when the user is online.
Add a data connection that submits the form as an attachment to an email message
Create or open an InfoPath managed-code form template.
In InfoPath design mode, on the Data tab, click Data Connections.
In the Data Connections dialog box, click Add.
In the Data Connection Wizard, click Submit data, and then click Next.
On the next page of the wizard, click As an email message, and then click Next.
On the next page of the wizard, type your email address in the To box.
In the Subject box, do the following to combine the sales period with the text Sales Report:
Click the Formula button next to the Subject box.
In the Insert Formula dialog box, click Insert Function.
In the Insert Function dialog box, click Text in the Categories list, and then double-click concat in the Functions list.
Replace the first instance of double click to insert field with the following (include the single quotes): 'Sales Report: '
Double-click the second instance of double click to insert field.
In the Select a Field or Group dialog box, select the period field.
Delete the final instance of double click to insert field, and then click OK.
In the wizard, click Next.
On the next page of the wizard, type 'Email Submit' in the Enter a name for this data connection box, and then click Finish.
Add logic for submitting the form depending on the connected state of a user's computer
In InfoPath design mode, on the Data tab, click Submit Options.
In the Submit Options dialog box, click Allow users to submit this form, and then select Perform custom action using Code.
Click the Edit Code button.
Add the following two functions below the OnSubmitRequest event handler:
public void OnlineSubmit(DocReturnEvent e) { // Logic for submitting online goes here. } public void OfflineSubmitX(DocReturnEvent e) { // Access and submit to the email adapter. DataAdaptersCollection myDataAdapters = thisXDocument.DataAdapters; EmailAdapterObject submitAdapter = (EmailAdapterObject) myDataAdapters["Email Submit"]; submitAdapter.Submit(); // Notify the user that the form was submitted offline. System.Text.StringBuilder message = new System.Text.StringBuilder(); message.Append("You submitted your Sales Report offline. "); message.Append("Your Sales Report is in your outbox "); message.Append("and will be submitted when you connect to "); message.Append("the network."); thisXDocument.UI.Alert(message.ToString()); // The submission was successful. e.ReturnStatus = true; }
Add the following if statement to the OnSubmitRequest event handler function.
// Check the computer's connection state. if (thisApplication.MachineOnlineState==XdMachineOnlineState.xdOnline) { OnlineSubmit(e); } else { OfflineSubmit(e); }
Test the code
In the InfoPath designer, click Preview on the Home tab.
Fill out the form.
Start Microsoft Internet Explorer.
In Internet Explorer, click Work offline on the File menu.
In InfoPath, click Submit. You should see a message that the form will be submitted as an email message.
Click Send. You should see a message stating that the form has been submitted offline.