14.2. Form Templates and Form Data

The InfoPath client reads the information in the form template to display the required views and controls based on the form schema. The user enters data into the controls and then saves the resulting data file either to the local drive or to a centrally accessible form data repository such as a SharePoint form library. The important point to remember here is that the form template is not copied to the form library when the user saves the data file. That only happens when the template is published to the library. (See the section on form publishing for a more detailed description of the publishing process.) Since only the form data file is stored into the form library, how does InfoPath know which template to use when the user opens the form?

Well, in the case of SharePoint form libraries, the simple answer is that InfoPath can look at the form library itself to determine which template is associated with the data file and then open the template from there, but form data files can also be stored locally or in a document library or some repository other than SharePoint. In those cases, the information about which template to use is taken from the form data file itself.

Say that you have a form data file on your desktop and you double-click the file to edit the data. InfoPath form data files contain a special processing instruction that tells InfoPath that the data represents an instance of an InfoPath form schema, and where the form template is located. Figure 14-2 shows an example of the processing instruction that is embedded into the form data file.

Figure 14.2. Figure 14-2

This means first and foremost that if the form template is moved from the location referenced in the file, then the data file may not open properly. Although InfoPath can adjust on the fly for most forms by caching the template on the development machine, you will get a warning when opening a template in design mode, as shown in Figure 14-3.

Figure 14.3. Figure 14-3

14.2.1. Working with Data Sources

The data source is the heart of any InfoPath form, and is really the best starting point when designing a form from scratch. Once the data elements are defined, it is easy to place them into views and arrange them on the page. It gets trickier when you need more than one data source in the same form. A form can have one primary data source, also called a data connection, and many secondary data sources that link to external data.

You set up the data sources for a form using the Data Source panel, which is only visible while designing a form.

Consider the common scenario where you want to let the user choose from a list of values in a dropdown list. You could enter the values directly into the control, but that wouldn't allow you to reuse the list in other controls. It is better to set up a secondary data source and then store the list values in a separate XML file that is packaged with the form.

Even though the XML file is stored within the form template file, it is still referred to as an external data source. Later, you can modify the data source or add a new one that retrieves the values dynamically from an external location, such as a SharePoint list, a web service or a SQL database.

When developing forms for SharePoint solutions, it is useful to create a data connection library to make it easier to connect your forms to dynamic data. To create a data connection library, navigate to a SharePoint site that will be accessible from your forms and select Create from the Site Actions menu, and then select Data Connection Library from the Libraries section. Figure 14-4 shows the New Data Connection Library page. Once you have created the library, you can begin populating it with data connections.

Figure 14.4. Figure 14-4

Although the Data Connection Library page includes both New and Upload commands, there is really only one command, and that is Upload. In other words, the only way to populate the data connection library is to first create a data connection file locally and then upload it to the library. The easiest way to create a data connection file is to use the Data Connection Wizard in Microsoft Excel 2007. To start the Data Connection Wizard, open Microsoft Excel 2007 and select Data From Other Sources From the Data Connection Wizard.

14.2.2. Connecting to SharePoint Data

One of the most common scenarios you will encounter when building forms for use in SharePoint solutions is pulling data from a SharePoint site to present users with a dropdown list of items to choose from while completing a form. A typical example is a list of departments in an organization that the user needs to select from. In the following example, you will modify the sample Asset Tracking form to enable a user to choose a department from a SharePoint list instead of typing in the department name.

  1. First, create a custom list on a SharePoint site named "Departments" and change the name of the default Title field to Department Name. You can populate the list with a few department names, as shown in Figure 14-5.

  2. Next, open the sample Asset Tracking form in design mode, as shown in Figure 14-6. The Asset Tracking form is one of the sample forms that are included with InfoPath. Notice the text control labeled "Department" associated with the employeeDepartment data field.

    Figure 14.5. Figure 14-5

    Figure 14.6. Figure 14-6
  3. Select Manage Data Connections... from the Actions section of the Data Source pane and click the Add... button to create a new connection. You will see the Data Connection Wizard.

  4. Choose Create a new connection to: and then choose Receive data and click Next.

  5. Select SharePoint library or list as the source of the data, and then enter the URL of the SharePoint site containing the custom list. You should now see the Departments list in the dialog, as shown in Figure 14-7.

  6. Select the Departments list and click Next to see the fields associated with the list, as shown in Figure 14-8. Place a checkmark next to the fields you want to include in the data connection. For this example, you only need to select the department name and click Next.

    Figure 14.7. Figure 14-7

    Figure 14.8. Figure 14-8
  7. The next dialog allows you to place a static copy of the data from the list into the form template for use by disconnected users. For this example, leave the check box deselected and click Next. Click Finish to complete the wizard and then click Close to return to InfoPath.

Now you have a secondary data source named Departments that is connected to a SharePoint list. To see the results in the form, you need to associate the data source with a control. To do that, execute the following steps:

  1. Right-click the TextBox control, which appears beneath the "Department:" label on the form, and select Change To Drop-Down List Box from the context menu.

  2. Right-click the control again, and choose Properties... from the context menu.

  3. In the Drop-Down List Box Properties dialog, select Look up values from an external source, in the List box entries section.

  4. Select Departments as the Data Source, and click the button to the right of the Entries text box.

  5. Select the :Department_Name field, and click OK. To filter out duplicate entries, place a checkmark in the Show only entries with unique display names field, as shown in Figure 14-9.

Save and preview the form. Now, when the form starts up, InfoPath attempts to connect to the SharePoint site in order to retrieve the data values for the dropdown list. When this happens, InfoPath issues a warning of a potential security issue as shown in Figure 14-10. Click Yes to skip the dialog. (See the section "Understanding InfoPath Security" for more details about the InfoPath security model.)

Figure 14.9. Figure 14-9

Figure 14.10. Figure 14-10

When the form opens, the dropdown list is populated with the current values stored in the SharePoint site. Figure 14-11 shows the result. As new departments are added to the list, they will appear in the form the next time it is opened.

Figure 14.11. Figure 14-11

14.2.3. Working with Existing Content Types

Often when working with SharePoint forms, you already have a good idea about the structure of the data, especially if it comes from an existing list or content type, but you don't want to link directly to an existing SharePoint list. Instead, you want to use an existing list as a model for the primary data source of the custom form that you are designing. It would be nice to have a way to simply reference an existing content type and then have InfoPath create the primary data source directly. Unfortunately, the current version doesn't let you do this. As a partial work-around, you can create a dummy form and create an external data connection to import the fields from an existing content type.

The steps are:

  1. Create a new custom list on a SharePoint site.

  2. Select Advanced Settings from the List Settings page, and enable content types for the list.

  3. Add the content type you are interested in to the list.

  4. Create a dummy InfoPath form. Any kind of form will do, even a blank form.

  5. Open the InfoPath Data Connection Wizard by selecting Manage Data Connections... from the Actions section of the Data Source panel.

  6. Click Add to create a new external data connection and then click Receive Data in the Create a new connection to: section.

  7. Select SharePoint library or list as the source of the data and click Next.

  8. Enter the URL of the SharePoint site that contains the list.

  9. Select the list that contains the content type, and place a checkmark next to the fields you want to use and click Next.

  10. Click Next to skip the part about storing a copy of the data.

  11. Enter the name of the content type as the name of the data connection, and click Finish and then Close.

You now have a secondary data source in your form with the same name as the content type. To complete the process, you would then cut and paste the data elements from the secondary schema into the primary data source.

Be Careful When Editing Schema Files

There are still some problems with this approach, however, that are illustrative of the way that InfoPath deals with data sources. First of all, the imported fields are all typed as strings. To correct this, you would have to go back through all of the fields one by one, changing each to the correct data type.

The second problem is that you cannot easily modify the XSD on disk once it has been generated by InfoPath. The prescribed approach is to select Save as Source Files from the File menu and then edit the files you want. InfoPath will recognize that the form was split into source files and will load them back in the next time the file is opened. This works well for certain files, like the XSL files for views. But if you edit the XSD files manually, you may run into problems if you make a mistake. It's easy to break a form this way, and it can be very time-consuming to get it back to a working state.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.241.116