How to: Work with Offline Solutions
The InfoPath object model provides the MachineOnlineState property of the Application class that enables your form code to check whether the user's computer is connected to the network. By checking the value of MachineOnlineState property, 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 e-mail message
Create an InfoPath managed code form template.
In InfoPath design mode, click Data Connections on the Tools menu.
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 e-mail message, and then click Next.
On the next page of the wizard, type your e-mail 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, click the Formula button next to the Attachment Name box, and then repeat the steps above to create the formula concat("Sales Report - ", period), and then click Next.
On the last page of the wizard, type E-mail 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, click Submit Options on the Tools menu.
In the Submit Options dialog box, click Allow users to submit this form, select Custom submit using form code, click Edit Code.
Add the following two functions below the Submit event handler:
public void OnlineSubmit(SubmitEventArgs e) { // Logic for submitting online goes here. } public void OfflineSubmit(SubmitEventArgs e) { // Access and submit to the e-mail connection. DataConnectionCollection myDataConnections = this.DataConnections; EmailSubmitConnection submitConnection = (EmailSubmitConnection)myDataConnections["E-mail Submit"]; submitConnection.Execute(); // Notify the user that the form was submitted offline. System.Text.StringBuilder myMessage = new System.Text.StringBuilder(); myMessage.Append("You submitted your Sales Report offline. "); myMessage.Append("Your Sales Report is in your outbox "); myMessage.Append("and will be submitted when you connect to "); myMessage.Append("the network."); MessageBox.Show(myMessage.ToString()); // The submission was successful. e.CancelableArgs.Cancel = false; }
Public Sub OnlineSubmit(ByVal e As SubmitEventArgs) ' Logic for submitting online goes here. End Sub Public Sub OfflineSubmit(ByVal e As SubmitEventArgs) ' Access and submit to the e-mail connection. Dim myDataConnections As DataConnectionCollection = _ Me.DataConnections Dim submitConnection As EmailSubmitConnection = _ DirectCast(myDataConnections("E-mail Submit"), _ EmailSubmitConnection) submitConnection.Execute ' Notify the user that the form was submitted offline. Dim myMessage As System.Text.StringBuilder = _ New System.Text.StringBuilder() myMessage.Append("You submitted your Sales Report offline. ") myMessage.Append("Your Sales Report is in your outbox ") myMessage.Append("and will be submitted when you connect to ") myMessage.Append("the network.") MessageBox.Show(myMessage.ToString()) ' The submission was successful. e.CancelableArgs.Cancel = False End Sub
Add the following if statement to the FormEvents_Submit event handler function.
// Check the computer's connection state. if (this.Application.MachineOnlineState == MachineState.Online) { OnlineSubmit(e); } else { OfflineSubmit(e); }
' Check the computer's connection state. If (Me.Application.MachineOnlineState = MachineState.Online) Then OnlineSubmit(e) Else { OfflineSubmit(e) End If
Test the code
On the Debug menu, click Start Debugging.
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 e-mail message.
Click Send. You should see a message stating that the form has been submitted offline and will be submitted when you connect to the network.