Collapsible Sections
Mark asks:
How do I create a collapsible section? That is, a section that can show/hide child controls, whether they are required or not. I can't use an optional section, because it requires binding.
The easiest way to do this is to this is using Conditional Formatting. Select the Section control, then pick Format | Conditional Formatting.... Add a rule where the formatting is “Hide this control”.
We often call this “conditional visibility.” Like everything in InfoPath, the view state is driven entirely by the data, so you need something in your data which maps to the expand/collapse state. One common approach is to add an attribute field to the group element the section is bound to. It shows up in your data when saved, and the expand/collapse state is saved when users save the form.
Bind a checkbox to the attribute, and you now have UI which lets the user control the state. If you prefer a button, you can create a rule which sets the value of the attribute; use two buttons with more conditional visibility rules to swap the label (e.g. between a + and a – to look like a TreeView). You can also write code which modifies the value of the attribute, and the section will show or hide.
Example 1: How do I restrict the user from entering special characters into certain fields in the InfoPath Form?
Note: This example assumes you are running InfoPath 2003 with the SP-1 feature enhancements enabled. [Added 6/21/04]
The easiest way to do this is to use pattern for data validation. For example, you might want a user to enter data in the ###-##-#### format for the Social Security Number field. If the user’s input does not match this format, you may want to throw an alert. You can achieve the above by doing the following:
Example 2: I cannot put a double quote (“) or an apostrophe (‘) in the pattern builder. How do I validate data against a pattern that contains a double quote (“)?
You can validate data against a pattern that contains double quotes by using Jscript regular expression. For example, you might want a user to enter data in the “###” format (a pattern contains double quotes) for a field. If user’s input does not match this format, you may want to throw an alert. You can achieve the above by doing the following:
function msoxd_my_field1::OnAfterChange(eventObj)
{
// Write code here to restore the global state.
if (eventObj.IsUndoRedo)
// An undo or redo operation has occurred and the DOM is read-only.
return;
}
// A field change has occurred and the DOM is writable. Write code here to respond to the changes
// Specify your pattern using Jscript regular expression
var re1 = new RegExp(“\x22\\d\\d\\d\x22”);
// Get the field value
var s = XDocument.DOM.selectSingleNode(“my:myFields/my:field1”).text;
// Find a match within string s
if (re1.exec(s) = = null && eventObj.Operation = = “Insert”)
XDocument.UI.Alert(“User input does not match the required pattern”);
The SP1 release of InfoPath 2003 supports recursive controls such as Repeating Recursive Section and Optional Recursive Section. Based on the schema of the solution, these controls are automatically suggested when dragging nodes from the Data Source task pane. This article talks about different types of recursion that are supported within InfoPath.
Direct Recursion:
This is the case when the node is recurring directly under itself. For example, if you are creating an organization chart, you might have a schema that has the following structure:
Employee*
EmployeeDataIDName…..Employee*
EmployeeData
IDName…..
Every employee has EmployeeData associated with him and can have 0 or more employees working for him. Note that the * stands for repeating 0 or more times. If you select the top level Employee node in the Data Source task pane, InfoPath would suggest a Repeating Recursive Section in this case with the recursive instance showing up directly under its parent.
Indirect Recursion:
This is the case when the node is recurring as a child of another node within itself. For the above organization chart, you may choose to abstract the fact that Employee is a manager or not by introducing an Optional node Manager. In this case the structure is as follows:
EmployeeDataIDName…..Manager?ManagerDataIDNameNumOfReports…..
Manager?
ManagerDataIDNameNumOfReports…..
ManagerData
IDNameNumOfReports…..
In this case, when you drag top level Employee node, InfoPath will create a Repeating Recursive Section that has the recursive instance within an Optional Section that corresponds to Manager node.
Potential Recursion:
This is the case when the node is recurring under as one of the choices under itself. Again taking the above organization chart example, we may choose to not include EmployeeData for managers since it is covered under ManagerData. So now we could have the following structure:
ChoiceNon-ManagerEmployeeDataIDName…..ManagerManagerDataIDNameNumOfReports…..Employee*
Choice
Non-ManagerEmployeeDataIDName…..ManagerManagerDataIDNameNumOfReports…..Employee*
Non-Manager
EmployeeDataIDName…..
Manager
Now we have an employee who can either be a manager or not. In this case, when you drag top level Employee node, InfoPath will create a Repeating Recursive Section that has the recursive instance within a Choice Section.
In summary, you have seen how InfoPath supports various types of recursion in its SP1 release. These three examples were just to illustrate the broad categories of recursion but you can go beyond these and try various other combinations based on your needs.
In the introduction to SP1, InfoPath added C# support. One issue that some people may run into is that certain OM calls have optional parameters. One such method is the SelectText() method in the View object. The context ID is optional and it not always easily found. In JavaScript of VBscript, the optional parameter would just be omitted and everything would work. In managed code (C# and VB.NET), you cannot omit optional parameters. To get around this, managed code has System.Reflection.Missing.Value which can be used in place of the optional parameter. This will achieve the same result as omitting optional parameters.
Microsoft Learning and Roger Jennings have a new book out through Microsoft Press - Introducing Microsoft® Office InfoPath™ 2003. This book covers InfoPath from top to bottom including the SP-1 feature enhancements and developing InfoPath solutions using managed code.
From the book's description:"Learn how to integrate forms with other Microsoft products such as Microsoft SQL Server and Access databases, Windows SharePoint Services, and XML Web services. This book also provides an overview of the technologies used to build an InfoPath forms solution using Visual Basic .NET managed code and the InfoPath Document Object Model (DOM)."You can find a little more information, including how to order, at http://www.microsoft.com/mspress/books/6511.asp.