We’re excited to announce that Darvish Shadravan and Laura Rogers’ Using Microsoft InfoPath 2010 with Microsoft SharePoint 2010 Step by Step (ISBN 9780735662063; 512 pages) is now available for purchase!
You can find the book’s introduction in this previous post.
In today’s post, please enjoy reading excerpts from Chapter 5, “Adding Logic and Rules to Forms,” and Chapter 8, “Using the InfoPath Form Web Part.” In Chapter 5, you’ll learn that the ability to adapt and respond in real time to user input is a major part of the InfoPath logic engine. The most common method you will use to add this type of logic to your forms is by using rules in InfoPath, of which there are three types: validation, actions, and formatting. In Chapter 8, you’ll learn how the InfoPath Form Web Part, which was introduced in Microsoft SharePoint 2010, allows you to insert a browser-based form directly onto a page on any site in SharePoint Enterprise or Microsoft Office 365.
Primary Types of Form Logic in InfoPath
Data validation rules help ensure that users enter data into your forms in an accurate and consistent manner, in exactly the format you intended. A common example is the use of data validation to ensure that a user types a Social Security number in a specific format (for example, 999-99-9999). This validation rule uses the matches pattern condition. You can use data validation to present the user with ScreenTips and error messages that help guide them to enter the data correctly. Other examples of data validation rules within forms could be ensuring that a user has entered an email address or phone number as you intended. As you can see in the screenshot below, most of the InfoPath data validation logic conditions are self-explanatory. These rules make adding validation logic to forms easy, even for the non-programmer.
Another type of rule that you can add to your forms is called an action rule. Actions can be triggered in your forms in a variety of ways; they are often initiated by user-driven events such as a button click. An example of an action would be setting the value displayed in a field based on a selection the user makes in a drop-down control in another part of the form. You can add multiple actions to a rule, making action rules extremely powerful tools. The primary types of action rules are briefly described below:
· Switch Views An action typically applied to a button or image that switches the user to another view in the form.
· Set A Field’s Value When you want the value in one field to be automatically determined by the value from another field.
· Query For Data Used when you need to run a query against a data source to submit or receive data.
· Submit Data Needed by most forms for publishing the form to SharePoint or some other location.
· Close The Form Often useful when you want your form to be automatically closed on the user’s computer after the necessary input has been provided.
· Send Data To Web Part A new Microsoft SharePoint 2010 Web Part, specifically for hosting InfoPath forms, that helps you send data between InfoPath and other Web Parts. We’ll cover this in more depth in a later chapter.
A third type of rule is called conditional formatting. This is a very useful type of form logic that allows you to manipulate the appearance of the form as the user fills it out. An example would be hiding an entire section of the form unless the user selects “yes” on a check box control. If they select “no,” the section remains hidden. Formatting rules allow you to create forms with dynamic interfaces that display only the relevant fields/sections. The primary types of conditional formatting rules are as follows:
· Font formatting (bold, color, and so on)
· Background color
· Hiding a control (including an entire section)
· Disabling a control
In addition to rules, InfoPath also supports formulas and functions for situations where you need to have more power and flexibility than the pre-defined rules allow for. InfoPath has functions that support a broad array of capabilities, such as date and time, math, and text manipulation.
Inserting a formula can be very powerful. The Formulas input box allows you to add fields or groups, functions, and operators that are used to calculate and display other values. As a quick example to illustrate this concept, the two screen shots below are from a simple form with just two fields: DueDate and ShipDate. In this case, what we want to do is have the user select a date from the DueDate field, which is a date picker control. Then the formula calculates the value of the ShipDate field by using the addDays function to automatically add 7 days. You can see how the InfoPath formula builder was used to easily build this logic.
InfoPath functions are not unlike those you might have worked with in Microsoft Excel or SharePoint. Functions typically return a value that is derived from the results of a calculation. Each function has values called arguments.
Functions generally fall into one of the following categories:
· Date and time functions
· Field functions
· Math functions
· Text functions
· URL functions
· User functions
Functions consist of three parts:
· Name Usually gives a hint as to the type of action that the function performs (for example, min, max)
· Return value The actual result of the function
· Arguments The specific values used by the function to perform a calculation
InfoPath functions/formulas can be used with default values and rules. You will have an opportunity to apply a few simple date and text functions in this chapter and then work with more complex scenarios in Chapter 14.
Web Part Settings
After the InfoPath Form Web Part has been inserted onto a page in SharePoint, the Web Part settings are used to configure it further. Not only can the specific form from the site be selected, but a form view can also be chosen, along with even more customizations regarding the display and behavior of the form.
In this section, each Web Part setting will be described. The following illustration shows an example of the Web Part tool pane for the InfoPath Form Web Part properties that has been added to a page.
· List Or Library This option allows for the selection of a form that has been previously published to the site. For a form name to exist in this list, one of the following conditions must be met:
o A SharePoint List on the current site has been customized to use an InfoPath form instead of the default list form.
o A form has been published directly to a form library on the current site.
o A form that was published as a content type or an administrator-approved template has been added as a content type in at least one library in the current site.
· Content Type If more than one form content type exists in the selected list or library, choose the name of the specific content type to be displayed in this Web Part.
· Display A Read-Only Form (Lists Only) This option can be used only if the selected form is associated with a SharePoint list, as opposed to a library. Choose this if the form simply needs to be displayed on the page and not filled out.
· Show InfoPath Ribbon Or Toolbar When this check box is selected, the contextual ribbon is enabled. This means that while the form is actively being filled out or selected, the ribbon at the top of the browser window displays context relevant to form submittal. The following illustration shows an example of the contextual toolbar when a list-based form is being filled out.
Tip When this Web Part option is selected, the interface on the page is a bit awkward, especially if the InfoPath Form Web Part is not close to the top of the page. When the form is being filled out, the site visitor would have to look at the top of the page, on the Edit tab in the ribbon, to find the Save or Submit button when the form is ready to be submitted. To avoid confusion during form submittal, this Show InfoPath Ribbon Or Toolbar box can be unchecked. In this case, a Submit button would need to be added to the form.
· Send Data To Connected Web Parts When Page Loads This check box is relevant only when Web Part connections are used in relation to the InfoPath Form Web Part. When filter data is being sent from this Web Part to another, any default value that exists in the form when the page first loads is sent to the connected Web Part. This results in the consumer (receiving) Web Part being automatically filtered to show that default value in the form.
· Select The Form View To Display By Default Select the desired form view to display in this Web Part. Read more about form views in Chapter XX.
Tip Because this Web Part will display the selected form view in its original size, a smaller view can be created specifically for use in this Web Part. In the form, create a new view and reduce the size by narrowing the tables and removing any unnecessary spacing. Learn more about views in Chapter XX.
· Submit Behavior After the form has been filled out and submitted, what needs to happen? Select one of the following options:
o Close The Form After the form is filled out, the form is replaced with the following gray text: The Form Has Been Closed.
o Open A New Form As soon as the form is submitted, a new blank form appears.
o Leave The Form Open The form is submitted to the list or library, but it remains open with no indication that the data was submitted. Site visitors might end up submitting the same form multiple times if it’s not obvious that the form submission occurred.
Tip If the Submit options in the Form Library form are not enabled, the Submit Behavior drop-down box will be disabled. In that case, the defined actions behind the form’s Submit button rules will apply.
In the following exercise, you will create a new SharePoint 2010 sample site and a simple form library form that will be used to capture website feedback from site visitors. This form will be used in the rest of the exercises in this chapter.
SET UP In the web browser, open your sample SharePoint site so that a new subsite can be created.
1. Click Site Actions, choose New Site, and create a site using the Document Workspace template; name the site Test Feedback.
2. From your computer’s Start menu, open InfoPath Designer 2010, and on the New menu (on the left), select SharePoint Library in the Popular Form Templates section.
3. Click Design Form on the right.
4. In the Fields pane on the right side of the screen, right-click myFields and choose Add.
5. Create the following fields listed in the following table by repeating step 4 for each, with Subject being the only required field.
6. In the main area of the form, use the Ctrl+A keystroke combination to select everything on the form, and use the Delete key on the keyboard to delete the form content so that the form is completely blank.
7. On the Insert tab on the ribbon, click to expand to see more tables, and select the table called Single-Column Stacked 2.
8. In the heading of the table you just inserted, type Feedback Form.
9. Narrow the whole table to a width of 410 px, using the Width button in the Layout tab. Click OK.
10. From the Fields pane on the right side of the screen, drag the Subject field to the next row of the table, under the heading.
11. Drag the Feedback Comments field to the next row down, and double-click to select this Feedback Comments text box. In the Properties tab of the ribbon, change the height of the text box to 50 px.
12. Click the Control Properties button, and in the Display tab of the text box properties, select the Multi-line check box and click OK. This ensures that if more than two rows of text are typed, the box will expand to accommodate more text.
13. Drag the Category field to the last row of the table.
14. The Category field needs to be a drop-down box instead of a text box, so right-click the text box and choose Change Control, and then choose Drop-Down List Box.
15. To add the category choices, click to select the Category drop-down box control. Use the Alt+Enter keystroke combination to quickly open the control properties.
16. In the drop-down list box properties window, use the Add button to add two choices: Suggestion and Complaint. Click OK.
17. Now the form is ready to publish to SharePoint. Click the File tab on the ribbon, choose Publish, and choose SharePoint Server.
18. Publish the form to a new SharePoint form library called Feedback Form on the site that was created at step 1.
For more information about details of the publishing process, please refer to Chapter 6. When you are prompted to save a copy of the XSN file to the file system, be sure to remember that location. On the Property Promotion screen in the wizard, add all three fields in the form to be promoted.
CLEAN UP Leave InfoPath Designer and the browser open, if you are continuing to the next section of this chapter. Otherwise, close InfoPath Designer and the browser.