One of my main jobs here at UWE is the creation of forms in InfoPath to gather information from users. This data is typically used in SharePoint to initiate a workflow, or may be used elsewhere, in a database.
When users fill out an InfoPath form, as with any web form, it is useful if you can pre-populate fields for them when the form opens. This saves time and also removes errors than can occur when people are in a hurry, or have lots of data to input. The most common item that people want to automate is filling in their own name when opening a form. Here at UWE we use Active Directory to authenticate users who use SharePoint, and as every user is pre-authenticated before they open the InfoPath form, it should be possible for me get the user’s name from their profile. And indeed it is.
InfoPath has the concept of Data Connections, both for reading data as well as writing data. Data connections can be to SharePoint lists, databases, XML files, or even web services. In this scenario I am going to show you how to query a web service on SharePoint to get the logged in user’s name, and then display it in the form.
First we need the InfoPath form. I am using InfoPath 2007 to create a simple form, this one is based on a template that has been designed by our Marketing people.
First I insert a control on the page so that the data has somewhere to go. Some people make the data connections first, I like to make the controls first then add the data later. In the picture below I have dropped text box control where I want the data to display.
Double clicking on the text box control brings up the text box properties box. Here you can give the field a meaningful name, and also set the default value for the text. I have changed the Field Name to txtName for now. We will come back to this Default Value item in a bit after we have set up the data connector.
On the menu, under Tools\Data Connections, you can create a data connection for that form template. Click on Add… to create a new item.
Here we are going to receive data. Click on Next. Choose Web Service from the options then Next to continue to the Web Service Details Page. Here I happen to know that SharePoint makes it’s user profile stuff available at http://yourservername/_vti_bin/userprofileservice.asmx?WSDL so that is what I put in the box. Put in the name of your own server here!
When you click Next you get a wide range of things you can query against. We are going to use GetUserProfileByName.

When you click Next you need to select the parameter you are going to search against. Here I am using AccountName. I expect you will do the same.
Enter a name for the data connection. It helps if you use a name that will make sense to you a year or so later when you come back to make changes!
Here I am using GetUserProfileByName. When you click finish you get a review screen that shows you what choices you have made.
To use the data connector, we go back to our textbox control on our InfoPath form and open the properties box. On the right hand side there is a function editor button. Click on the button to get the insert formula window.
Right, here is where it gets complicated. You cant just copy and paste text into here. This is a dynamic editor that takes values and builds xpath strings. Pasting or typing the text in will not work, you need to click on button then type values.
I want to insert field or group, so I click on the left button. This gives me a choice of data sources. Main is selected by default. You change it here to the source that we just made, GetUserProfileByName.
Now you have to decide what repeating section to use. The section you want is Values:
Now that will not give us a single value. To get the users name we have to filter the data. To do that click on the Filter Data button. Easy innit? Next click on Add… to specify the filter conditions.
Here I know that the value that I want from AD is the PreferredName, so I type in:
Clicking OK, then OK, then OK, then OK takes us back to:
Then OK again:
And again (that's a lot of OK’s) takes us back to the form. Here you can click “Preview” to run up the form and test (you have to be logged in on your domain for this to work. It doesn't work remotely…
And there you have it. The currently logged in users Preferred Name automatically appears in the text box. You can use this method to get at other values too, not just this rather easy example.
There are easier ways of getting this information, but this is a good way of learning how to get under the hood of InfoPath to get at data that you might not know is there. Later on I will show you methods that use code in the same way, but that is best left for another time!