(This post applies to InfoPath 2007. If you're using InfoPath 2010, then you should check out this post instead.)
We have seen a number of requests on how to allow a user to enter (or select) a person’s name or logon alias and then validate that user against their Active Directory without using code. This has been especially true in workflow scenarios where User A needs to enter in the name of User B – the next person in the workflow process.
Well, InfoPath 2007 ships with a control called Contact Selector Control that will resolve these issues! You may have seen our older article on the subject; this one aims to dive in deeper.
The Contact Selector control is an ActiveX control but it is a special cased control, in that it can also be used in InfoPath browser forms. To use this control there are specific steps that need to be taken – let’s take a look at those now.
Step 1: Add the Contact Selector control to your Controls Task Pane
1) From the Controls Task Pane click the Add or Remove Custom Controls link
2) Click the Add button
3) On the first screen of the Add Custom Control Wizard select ActiveX control and click Next
4) From the list of controls, choose Contact Selector and click Next
5) Select “Don’t include a .cab file” and click Next
6) For Binding Property select Value and click Next
7) From the Field or group type box choose Field or group (any data type) and click Finish
8) Click Close and then click OK
Step 2: Create the data structure for the Contact Selector Control
The Contact Selector control needs to have a specific data structure to work properly – this is documented on the “Items” tab of the Properties screen for the control; however, we’ll include that information here as well.
**IMPORTANT!** Spelling and capitalization must be exactly the same, starting with the “Person” group!
1) Add a non-Repeating Group named: gpContactSelector
2) Add a Repeating Group named: Person
3) Add the following 3 text fields to the Person group: DisplayName, AccountId and AccountType
Step 3: Add and bind the Contact Selector control to the View
1) Drag the gpContactSelector Group to the View and select “Contact Selector” from the list of controls
2) You’re almost done…! :-)
Step 4: Add a secondary data source XML file which specifies the SharePoint server
The Contact Selector control needs to know the “context” of where the user validation should occur. These steps are not necessary if you are only displaying the form in a browser from SharePoint – in this case, it uses the context of the site from where it was provisioned; however, if you are in a mixed client/browser scenario you will need to include this XML file so forms opened in the client can use this functionality.
1) Launch Notepad
2) Copy and paste this one-line XML:
**NOTE: Replace <servername> with the name of your server
3) Save this as: Context.xml (again – naming and capitalization are important)
4) Add Context.xml as a “Receive” type Secondary Data Connection to your form template and make sure the option “Include the data as a resource file” is enabled
Step 5: Test!
You should now be able to Preview the form, enter a name or logon alias, click the “Check Names” button and resolve the name! Alternatively you could click the “To” button to perform a Search if you do not know the complete name of the user.
One other important point: if this control is something you will use numerous times, this process works great to create a “Contact Selector Template Part” – then you only have to complete these steps one time!
Scott HeimSupport Engineer
Hi Scott -
I need help with throwing an error message to the user when they try to enter multiple contacts. I read your blog entry where this was covered above - however, I am not sure where to put the code. Would this be part of the Submit options? I couldn't find a place in there either.
The code shows where it goes:
** NOTE: "group1" is the parent node of the "Person" repeating node. **
public void group1_Validating(object sender, XmlValidatingEventArgs e)
if (e.Operation == XmlOperation.Insert)
if (e.Site.SelectChildren(XPathNodeType.Element).Count > 1)
e.ReportError(e.Site, false, "User Selection Error!", "Only 1 user can be selected!");
Using the sample from the post, "group1" is the parent node of the "Person" repeating node. So as you see in the code, there is a "Validating"event on group1. What you need to do is:
- Display the Data Source Task Pane
- Right-click on group1, select Programming and then choose Validating Event
You will then get an empty event. Add the following code to that event:
Thanks, I will try it, right now this option is grayed out until I get the VSTA downloaded and I am unsuccessful in doing so, I keep getting redirected to all kinds of links.
On another note, during form field promotion, I am trying to promote one of the fields in the gpContactSelector to an existing site column called Associate Name that will show Presence. I am trying to merge the DisplayName into the Associate Name column and receive this error:
"The data type of the selected site column does not match the data type of the field to promote" - I also tried AccountId and AccountType, none of them work.
Do you know a workaround?
Unfortunately I don't know of a workaround other that what I suggested earlier in this blog - the difference is that field will simply be a text field containing the AccountId or DisplayName of the user, which means Presence will not be available.
Para que septiembre no se vaya en blanco, aqui les comparto algo que nos anduvo dando algunos dolores...
I added the Contact Selector web part to a browser form, and I can select users properly from the sharepoint hosted environment.
However when I go to send an email using the contactSelector group as the data field, it tries sending to email@example.com instead of the proper ad email address which is firstname.lastname@example.org, am I pointing to the wrong part of the datasource or am I doing something wrong here?
When you select a user in the Contact Selector, is the name returned as first.lastname in the control?
Also - drag that node onto your view as a Section with controls so you can see each node and make sure DisplayName and AccountId show the same information.
Hello InfoPath Gurus,
I am following the directions you outlined for caseycase above and the Contact Selector automatically populates with the resolved name to the current user when the form is opened (Perfect, thanks.). The problem I’m experiencing is when a user enters a new name and resolves it, the returned value is: <currentUser> <newName>, even if the current user value is cleared from the Contact Selector before the new name is entered. Is your solution above meant to allow for a different name to be entered (the traditional use of the Contact Selector) or does it limit the control to the current user with appended names? Thank you for any information you can provide. - K
I see what you are saying...sorry about that. Try these modifications:
- Remove the userName() function from the DisplayName field
- From the Tools menu choose Form Options -> Open and Save category
- Click Rules (you should see one rule created above)
- Click Modify
- Click Add Action
- Action: Set a field's value
- Field: Pick the DisplayName field from your main data source
- Value: Click the "fx" button and insert the userName() function
- Make sure this new action is the first item in the list of actions
This will still populate the "default" value to be the user that opened the form but if the user enters a new value in the control, it now appears to resolve correctly.
Your solution seems to work fine until I apply the validation code that you supplied above for limiting the control to a single entry. Somehow the ResolvePrincipals method is incrementing the person count on the Contact Selector, and not showing the accurate count after the first person is removed and replaced. Is there a way to force the count back down to 0 when the field is cleared? Thanks again for all your help. – K
I don't believe I am seeing the same behavior - here is what I tested:
- Made the modifications noted above to the Rule on the Form Open
- Added the code to insure only 1 user could be added
- Launched the form - result: my user name is populated and resolved
- Deleted myself from the Contact Selector and entered a co-workers name
- Clicked the Check Names button - result: name is resolved and no error about the number of users
Am I doing something different?
I apologize Scott,
Somehow I was applying the validation code to the “Person” group rather than the Contact Selector group. I was too quick to post. Sorry! :-) This is a great thread. Thank you for responding so quickly and thoroughly. InfoPath can be a bear for beginners, but it would be impossible without these forums. Thanks again. – K
No problem - I am glad it works for you!
I have problems when I try use CS with TNS name space.
Do You know how fix it?
A few more details would be helpful. What have you done so far, what error are you receiving, does this sample even work, etc.