Everyone likes InfoPath's email data connection because it lets you collect forms using email only, no other infrastructure required (no need for Windows SharePoint Services, SQL Server, or even a file share). We've built even more Outlook integration in InfoPath 2007 Beta, but since most of you don't have that yet, let me share a tip that will work in both InfoPath 2003 and 2007.
The basics: Single dynamic email address
As your probably know, the To and CC line of the email data connection can come from a textbox in the form by using a formula. To do that, just use the Fx button next to the To line in the data connection wizard:
The trick: Multiple email addresses from repeating controls
Some forms have a list of names they want to send to, but the simple formula above won't work for that.
For example, consider a repeating table that looks like this:
With this data source (note that "person" is repeating):
So you want to produce this semicolon-separated list of e-mails:
A good instinct is to use the "concat" function, but unfortunately that only works on the first element in a repeating structure.
So then comes the team insight: Our "eval" function returns a list of nodes which actually share an anonymous parent. That means you can use one eval functions to create a list of the email addresses, then wrap it in another eval function that gets the parent of that list.
Voila, here's the formula to solve the problem:
eval(eval(person, "concat(my:email, ';')"), "..")
(Note that "person" can be inserted from the data source, but "my:email" needs to be typed by hand or you'll get an error.)
For the curious: Here's how it's done
Let's break down that XPath formula from the inside out:
"concat(my:email, ';')" - Adds a semicolon to each email address.
eval(person, "concat(my:email, ';')") - Loops through each person to create a list of email addresses
eval(eval(person, "concat(my:email, ';')"), "..") - Gets the anonymous parent of the email addresses, and converts them to a string.
So the end result returns the contents of that anonymous parent, which is a series of semicolon-delimited email addresses. Phew!
We are using two tricks here:
- The fields returned by eval() all have the same anonymous parent (feature of InfoPath's function)
- The string value of a parent is the concatenation of all its children (W3C spec’ed)
- David Airapetyan (Software Design Engineer) and Ned