Share via


Data Connections in Template Parts

Template parts allow a form designer to combine view information, rules, schema, default data and other compatible components of a form template into a package that can be distributed to other form designers and inserted into form templates without manual recreation. Data connections, with some restrictions, are a compatible component that can be included in a template part.

Adding a data connection to a template part is the same process as adding a data connection to a form template, with one exception - data connections in template parts cannot submit data. Once added to the template part, the data connection is added to the form template upon insertion. When inserted, the data connection name will be appended with the template part name, identifying the data connection as originating from the template part. The unique name of the data connection is used to identify the data connection during insertion and updating of the template part. When a template part with a data connection is inserted, the data connections are searched for the data connection name. If found, the data connection is removed and replaced with the data connection in the template part. When updating a template part with a data connection, the previous data connection name is searched, and if found, is removed and the data connection associated with the template is inserted. This gives the template part designer the ability to update the data connection when required to support changes made to the template part. For the template part user, this ability causes behavior that may feel counterintuitive:

  1. Multiple insertions of the same template part with data connections unbinds the controls in the previous template parts inserted due to the replacement of the data connection on insert.
  2. Any changes the user makes to a data connection installed as part of a template part insertion is lost when the template part is updated.

In the case of inserted multiple instances of a template part with a data connection, the controls can be easily rebound after insertion. To prevent an update of a template part from changing the data connection, rename the data connection so that it is not found by the template part on update. There may be other cases where this behavior causes unexpected results. Awareness of this behavior should assist template part designers and users in identifying the cause and resolving it.

The following example demonstrates adding a data connection to a template part (please note - the third party web services availability may not be consistent); I'm also attaching a template part that you'd get by following the instructions below.

  1. Create a new blank template part
  2. Click Data Connections… from the InfoPath Tools menu
  3. Click Add…
  4. Verify Receive data is selected and click Next
  5. Select Web service and click Next
  6. Enter the web service http://ws.invesbot.com/stockquotes.asmx?wsdl and click Next
  7. Select GetQuote and click Next
  8. Click Set Sample Value… ,enter MSFT and click OK
  9. Click Next 3 times
  10. Uncheck Automatically retrieve data when form is opened
  11. Click Finish on Data Connection Wizard
  12. Click More Controls… from the InfoPath Insert menu
  13. Click on a Button control to insert it
  14. Right click on the Button control and click Button Properties…
  15. Set Action to Refresh and click OK
  16. Click Data Source… on the InfoPath View menu
  17. Select the GetQuote (Secondary) data source from the Data Source pane
  18. Expand the data source down to the following field: myFields>queryFields>tns:GetQuote>symbol
  19. Drag the symbol field onto the canvas (a text box control should be created)
  20. Expand the data source down to the following field:
    myFields>dataFields>tns:GetQuoteResponse>GetQuoteResult>StockQuote>Open
  21. Drag the Open field onto the canvas (a text box control should be created). The template part should look similar to this:
  22. Save the template part as StockQuote.xtp

The template part now contains a data connection. To make the template part available to the form template design mode, do the following:

  1. Create a new blank form template
  2. Click More Controls… from the InfoPath Insert menu
  3. On the Controls pane click Add or Remove Custom Controls…
  4. Click Add…
  5. Select Template part and click Next
  6. Select the StockQuote.xtp file through Browse... and click Next
  7. Finish the Add Custom Controls wizard
  8. On the Controls pane a new control named StockQuote should appear under Custom
  9. Click StockQuote to insert it

The template part is now part of the form template. Save and preview the form. Type MSFT in the Symbol text box and click the Refresh button (you may need to click Yes to a security dialog box). The Open text box should contain the requested data.

Richard Witte
Software Test Engineer

StockQuote.xtp

Comments