Working With InfoPath Controls on the Design Surface and Building a Basic InfoPath Form
The InfoPath design surface provides many methods to build and enhance an InfoPath form with custom actions such as conditional formatting, rules, and other formidable functionality. There are several options available from the control level menu that a developer can leverage to circumvent having to write custom code to achieve certain form tasks. We have already explored how to use a template part in order to build a reusable InfoPath form piece similar to user controls in Visual Studio development that can be dragged onto the InfoPath design surface after a user has imported the .XTP file. Building a custom form with the native InfoPath controls is no different, and can be accomplished within the same amount of steps. For our purposes, we will explore some of these inherent surface design options to create a simple form that will query user ID’s from a SQL database. It is important to realize that if extending this form to submit user ID’s to a database, a Web Service has to be generated for the database since a Web Form cannot submit to the SQL database directly, which is why in the Main Data Connection Details the Type is set to retrieve data from the database. Although all of the inherent features will not be presented, there are some indispensable pieces of functionality that are intuitive to use within an InfoPath form which can be seen in the following list
- Changing Data Binding. When working with the data binding options on the InfoPath design surface, each of the controls is bound to certain fields within the data connection source. At times, it may be beneficial to change the binding of the control to a different field or group. Changing the data binding allows you a visual selection of the available fields and groups to which the selected control can append itself.
- Conditional Formatting. Conditional formatting allows you to wrap logic around a control where the display formatting will be adjusted based on whether conditions are met. This same type of feature is available within SharePoint XSLT data views. Conditional formatting generally works by building up an XSL if/when statement that will output the control field data with the rule satisfaction criteria.
- Data Validation. Data validation provides a method that the InfoPath control can use to verify that input data conforms to the rules implemented by the validation method.
- Rules. A rule directs InfoPath to take an action when a specified condition is met. The first step in creating a rule is to give it a friendly name. Next, the conditions of the rule have to be set. Lastly, the actions that should be taken when the conditions are satisfied have to be set, which can be things ranging from displaying a message to a user to querying values out of a data source.
- Programming. The programming menu allows developers to enter the VSTA development environment to add different event handlers to the InfoPath control, such as changing, validating, and changed events.
- Logic Inspector. The logic inspector will allow you a view into all the different pieces of functionality that are set on a control such as validation, calculated values, rules, and programming.
In order to build the basic InfoPath ID form, select Design a Form Template from the File Menu. On the New Form Template screen you can create several types of templates based on the data connections that you wish to use which will create a boilerplate form.,. If desired, you could optionally create a blank InfoPath form template and add the relevant data connections in order to iterate through external datasource values, for more information regarding data connections see the next section. For the ID Form, the values are going to be stored in a SQL database and the form will be available to the users on the web. Select Database and Enable browser-compatible features only to ensure that we are backward compatible with the controls being used on the form (See Figure14-12).
Once the form has been defined with the data connection that it is going to used, InfoPath requires more information in regards to where the data should be massaged from. Although this type of activity is also manageable from code, choose the Select Database option in order to point to the ID database that only has an ID column in it. The SQL statement that is generated is fairly simple in order to get the values out of the database, it should be simply select “ID” from “dbo”.”ID” as “ID”. This will translate the ID query into the comparable XPath statements for the query fields /dfs:myFields/dfs:queryFields/q:ID/@ID and for the data fields /dfs:myFields/dfs:dataFields/d:ID/@ID.
When creating a new InfoPath Form based on a database connection, InfoPath will attempt to parse out methods that are required for you to interact with the database. You can either use the Run Query options which will allows you to run the desired query or simply Right-click on the ID field from the Data Source task pane, and select the control that you want to format the dataset as, in this case instantiating a control for the ID node. This will put the desired control onto the design surface based on the node from the desired dataset.
Once you can see that the form is receiving data from the SQL database correctly, you can use the other inbuilt InfoPath controls in order to enhance the form with validation and logic. Right-click on the ID Text Box control, and select Conditional Formatting. Using Conditional Formatting you can hide this control if there are no ID’s present when attempting to iterate through the dataset received from the database used in order to populate the control. In order to set up the rule, select the ID field and create the condition is blank. Afterwards, set the action to Hide this Control. (See Figure 14-15)
The final results should show the summary of the formatting conditions (See Figure 14-16).
Using Data Validation is a powerful option in order to verify the data that is placed into an InfoPath control. The data validation that is presented is very broad and powerful user interface piece of functionality in order to inform the user that the data that they are entering is of appropriate form. Right-click on the ID Text Box control, and select Data Validation. Once the validation dialog is open, setup a new validation rule that will display A bad ID has been returned message to the user when the number 1 is entered into the textbox (See Figure 14-17).
Then, as you can see when 1 is entered into the ID textbox it will inform the user that a bad ID has been entered (See Figure 14-18).
Rules are a compelling option that you can use in order to set a condition, and once the condition is met to execute some sort of action. The actions that can be taken when executing a rule are:
- Show a Dialog Box Message
- Show a Dialog Box Expression
- Set a Fields Value
- Query Using a Data Connection
- Submit Using a Data Connection
- Open a New Form To Fill Out
Similar to the Data Validation that was previously setup, rules are method that can be used in order to display a message box to the user if a certain condition is met, but can be extended much farther to include more custom logic. In order to see the execution of a more complex rule, examine the setting of an alternative control when the primary control value meets a condition. Drag another Text Box control onto the design surface, and set the Field Name property to Copy Test. Once this control is on the page, Right-click on the ID Text Box control, and select Rules. Create a new Rule that will trip when the Text Box value entered equals 1. In the rule dialog, set the action so that it will set the Copy Test field value to string Copy_Test when the condition is met (See Figure 14-19).
Once all of these settings are made on the TextBox, you can view the changes that have been made using the Logic Inspector which will provide an inventory of all of the customizations that have been made for the InfoPath form (See Figure 14-20). This list can then be printed out in order to maintain an audit interface of all the customizations that have been added