How to: Work with Offline Solutions Using the InfoPath 2003 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 e-mail message

  1. Create or open an InfoPath managed-code form template.

  2. In InfoPath design mode, on the Tools menu, click Data Connections.

  3. In the Data Connections dialog box, click Add.

  4. In the Data Connection Wizard, click Submit data, and then click Next.

  5. On the next page of the wizard, click As an e-mail message, and then click Next.

  6. On the next page of the wizard, type your e-mail address in the To box.

  7. In the Subject box, do the following to combine the sales period with the text Sales Report:

    1. Click the Formula button next to the Subject box.

    2. In the Insert Formula dialog box, click Insert Function.

    3. In the Insert Function dialog box, click Text in the Categories list, and then double-click concat in the Functions list.

    4. Replace the first instance of double click to insert field with the following (include the single quotes): 'Sales Report: '

    5. Double-click the second instance of double click to insert field.

    6. In the Select a Field or Group dialog box, select the period field.

    7. Delete the final instance of double click to insert field, and then click OK.

  8. In the wizard, click Next.

  9. On the next 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

  1. In InfoPath design mode, on the Tools menu, click Submitting Forms.

  2. In the Submitting Forms dialog box, click Enable Submit commands and buttons, and then in the Submit to box, select Custom submit using form code.

  3. Select the Edit Form Code check box, and then click OK.

  4. 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 e-mail adapter.
       DataAdaptersCollection myDataAdapters = 
       EmailAdapterObject submitAdapter = 
          (EmailAdapterObject) myDataAdapters["E-mail 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.");
       // The submission was successful.
       e.ReturnStatus = true;

  5. Add the following if statement to the OnSubmitRequest event handler function.

    // Check the computer's connection state.
    if (thisApplication.MachineOnlineState==XdMachineOnlineState.xdOnline)

Test the code

  1. In the Solution Explorer, right-click the project, point to Preview, and then click Default.

  2. Fill out the form.

  3. Start Microsoft Internet Explorer.

  4. In Internet Explorer, click Work offline on the File menu.

  5. In InfoPath, click Submit. You should see a message that the form will be submitted as an e-mail message.

  6. Click Send. You should see a message stating that the form has been submitted offline.