Generate a concatenated string of email addresses from a list using VBScript

Applies to: InfoPath 2003

Use VBScript to extract email addresses from a SharePoint list of contacts and generate a concatenated string of email addresses separated by a semi-colon.



You have a SharePoint list that stores the first name, last name, and email address of contacts. You would like to use the email addresses to generate a concatenated string of email addresses separated by a semi-colon like in;;


Retrieve the data connection to the SharePoint list, extract all the contacts, and use their email addresses to generate a concatenated string of email addresses.

If you are using a repeating table or a people picker in a repeating table, and do not want to write code, you can also use the eval() and concat() functions to concatenate data from a repeating table.


You can accomplish this functionality as follows:

  1. You have a SharePoint Contacts list with the columns LastName, FirstName, and E-mail Address.

    Figure 1. SharePoint Contacts list.
  2. Create a New Blank Form in InfoPath.
  3. Create a Data Connection to the SharePoint list, select the LastName, FirstName, and Email-Address to be included in the data connection, and call the data connection Contacts.
  4. The XML data for the SharePoint list used in this solution resembles the following:
    <dsf:Contacts Last_Name="Jones" First_Name="Andruw"
    <dsf:Contacts Last_Name="Jones" First_Name="Chipper"
    <dsf:Contacts Last_Name="Renteria" First_Name="Edgar"
    <dsf:Contacts Last_Name="Smoltz" First_Name="John"
    Note: Namespace declarations have been left out to increase clarity.
  5. Add a Table with Title to the InfoPath form.
  6. Add a List Box control to the table and bind it to the Data Connection for the SharePoint list. Use the LastName field as the Value as well as the Display name.
  7. Add a Text Box control to the table and name it emailAddresses.
  8. Modify the table and controls to resemble the following figure:

    Figure 2. The InfoPath form in design mode.
  9. Go to Tools > Options..., click on the Design tab, and make sure that the Default programming language is set to VBScript.
  10. Go to Tools > Programming > On Load Event... to add an OnLoad event handler to the form. Add the following code to the OnLoad event handler:
    Dim objContactsDOM
    Dim objContacts
    Dim emailAddress
    Dim emailAddresses

    ' Retrieve the DOM for the secondary data source connected
    ' to the SharePoint Contacts list
    Set objContactsDOM = XDocument.GetDOM("Contacts")

    ' Set the namespaces to be used when retrieving XML nodes
    objContactsDOM.setProperty "SelectionNamespaces", _
    "xmlns:dfs="""" " & _

    ' Retrieve all the Contacts nodes
    Set objContacts = objContactsDOM.selectNodes("/dfs:myFields/dfs:dataFields/dsf:Contacts")

    ' Loop through all the contacts
    For i = 0 To objContacts.length - 1

    ' Retrieve email address for contact
    emailAddress = objContacts(i).selectSingleNode("@E-mail_Address").text

    ' Generate concatenated string of email addresses
    If (emailAddresses = "") Then
    emailAddresses = emailAddress
    emailAddresses = emailAddresses & ";" & emailAddress
    End If


    ' Set the value of the emailAddresses field to the concatenated string
    ' of email addresses
    XDocument.DOM.selectSingleNode("//my:emailAddresses").text = emailAddresses
    This code ensures that whenever the form is loaded, the data in the secondary data source will be used to generate a concatenated string of email addresses from the SharePoint Contacts list.
  11. The form should resemble the following figure when it is opened:

    Figure 3. The resulting InfoPath form.

Related InfoPath Articles:


Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author.