No Code InfoPath Trick

On July 23, 2012, in Insights, by Kirk Liemohn

Kirk Liemohn shows how to work around when you’re not able to use code in an InfoPath form and still use Email data connections. Continue reading

On a recent project I was unable to use code within my InfoPath form because once you use code in an InfoPath form you are using Forms Services, which then means you can no longer use Email data connections. However, I needed Email data connections. In this blog post, I’ll share how you can work around this issue.

Imagine you have an InfoPath table of information (loaded from a SharePoint List, of course) that has email addresses in it:

NameEmail
Lucky Daylucky.day@somecompany.com
Dusty Bottomsdusty.bottoms@somecompany.com
Ned Nederlanderned.nederlander@somecompany.com

Say these are in the InfoPath data as the following (ignore my:contactEmails for now):

my:myFields
my:contacts
my:name
my:email
my:contactEmails

At some point, I want to send all of these contacts an email using an Email data connection. At first I tried setting the To field to /my:myFields/my:contacts/my:email, but the value came out as everything concatenated with no delimiter:

lucky.day@somecompany.comdusty.bottoms@
somecompany.comnednederlander@somecompany.com

I was hoping to find an easy way to split/join this together like I am so comfortable doing with c#. If that’s possible, I couldn’t figure it out, so on to some hacking…

Since you cannot iterate through elements without code (and code is not allowed in my case) I was at a disadvantage. In addition, I didn’t want to do a horrible hack of assuming n number of contacts and manually concatenating them together (that was plan Z, though).

Here’s what I did:

  1. Created my:contactEmails under the my:contacts repeating group and set its default value to:
    concat(../preceding-sibling::my:contacts[1]/my:contactEmails, ";", ../my:email)
    
  2. Created another field called my:contactEmailHack (not under a repeating group) and set its default value to:
    substring-after(/my:myFields/my:contacts[count
    (/my:myFields/my:contacts/my:name)]
    /my:contactEmails, ";")
    

Basically I’m telling each my;contactEmails value within the table to have the previous row’s “contactEmails” value plus “;” plus the “email” from the current row (step 1). Then I’m looking at the last row for the total set of concatenated emails, but removing the initial semicolon (there is bound to be one because the first row has no previous row). I would have used last() instead of count(), but alas, last() is not allowed in web browser forms.

InfoPath has a lot of “tricks of the trade”. Hopefully this one can be applied for various needs that would normally be handled by iterating through rows of table. If you know of any better way of doing this without code I’d love to hear your solution!

Thanks to Kjell-Sverre Jerijærvi’s blog post for helping me properly get the preceding sibling.

Tagged with:  
1 comments
dannyryan
dannyryan moderator

 @kliemohn - thanks for keeping the blog posts going...I know everyone has been so busy on projects that it's tough to get these down on paper. I really appreciate you sharing.