When building a workflow solution using InfoPath, it is often necessary to enforce data validation for some, but not all users. For example, in a simple expense report workflow, Employee->Manager->Accounting, the manager may be required to specify the cost category. The employee, however, can’t provide this data. This means that setting the “cannot be blank” property for the costCategory field in the InfoPath form will not be enough to make this work. In this article, we will explore a technique that will let you use powerful InfoPath data validation capabilities to enforce this business logic correctly.
You may have seen a different incarnation of the same problem – “if only I could submit the form with data validation errors…”. If so, read on.
- Create a node in your data source that remembers the role of the current user; let’s call that node userRole. Create rules or code to get that node populated on load of the form.
Trick 1 – acquiring user role. To set the value of a node to the current user role in rules, use the XPath expression xdXDocument:get-Role().
Trick 2 – acquiring Active Directory username. If you are using InfoPath 2007, you can get to the username of the current user through the built-in userName function. In InfoPath 2003, the easiest way to accomplish this is to write a simple whoAmI web service (here is a sample implementation), and plug it in to your form template through a secondary data connection.
- Setup data validation logic on the costCategory field by using the Data Validation dialog (not through the “cannot be blank” shortcut in the properties dialog).
- Add a new “logical and” clause to the data validation condition. This clause will check if the userRole actually requires the field to be filled out. The entire data validation condition will evaluate to false if the userRole doesn’t have this requirement, and the error message will not be shown:
This technique will work in both InfoPath 2003 and InfoPath 2007, and it is supported in browser-enabled form templates.