Programmatically check whether a value exists in a drop-down list box bound to a SharePoint list using an XPath filter

Learn how to use an XPath filter expression on a SharePoint list to look up a value in a secondary data source to which a drop-down list box in InfoPath is bound.

ADVERTISEMENTS

Problem

You have a drop-down list box in InfoPath, which is bound to a SharePoint list that contains
names and email addresses of contacts. The values of items in the drop-down list box are bound
to the IDs of the contacts and the display names of items in the drop-down list box are bound
to the Last Names of the contacts.

You want to be able to type the email address of a contact into
a text box on the form template, use this email address to check whether the contact exists in the
drop-down list box (so in the SharePoint list that the drop-down list box is bound to), and if the
contact exists, have this contact automatically appear
as the selected contact in the drop-down list box. If the email address does not exist, no contact
must be selected.

Solution

Use an XPath filter expression on the SharePoint list to find the contact whose
email address is equal to the given email address. If found, retrieve the ID of this contact and then set
the value of the drop-down list box to the ID of the contact.

Discussion

You can accomplish this functionality as follows:

In SharePoint, ensure that you have a Contacts SharePoint list that you can use. This list must contain IDs, names, and email addresses of contacts.

Add a Drop-Down List Box control to the InfoPath form template and bind it to the data connection for the SharePoint list.
Set the @ID of the contact to be the Value of an item in the drop-down list box and set the
@Last_Name of the contact to be the Display name of an item in the drop-down list box.

Name the Drop-Down List Box control contact.

Add a Text Box control to the InfoPath form template and name it email.

Right-click the Text Box control and choose Programming and then Changed Event from the context menu.

Add the following C# code to the Changed event handler of the Text Box control:

// Check whether the search returned a result and
// if it did, retrieve the ID of the contact
string contactID = string.Empty;
if (contact != null)
{contactID = contact.SelectSingleNode("@ID", NamespaceManager).Value;
}

' Check whether the search returned a result and
' if it did, retrieve the ID of the contact
Dim contactID As String = String.Empty
If contact IsNot Nothing ThencontactID = contact.SelectSingleNode("@ID", NamespaceManager).Value
End If

Note: MyContacts in the example code above is the name of the data connection (secondary data source) to a
Contacts SharePoint list.

Save your work, build the project, and test the form.

Now whenever you type an email address into the text box and click or tab away from the text box, the email address is
looked up in the secondary data source connected to the SharePoint list by filtering the list on that email address,
and if it is found, the ID of the contact
pertaining to the email address is retrieved and then used to set the selected item in the drop-down list box.

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.