Mar 19 2009

Auto-filling InfoPath 2007 Forms with User Profile Data

InfoPath 2007 Web based forms are great… and can be even better when they are configured to be “user aware”

In this article we will see just how easy it can be to make your Forms automatically fill in known information about the person filling out the form from the SharePoint User Profile Database.

In the interest of being more interesting with less filler… We will start with an InfoPath Form that contains a 3 Row, 2 Column Table with labels as in the following drawing and a text field and date field as in the following drawing

Basic Form

 

(Note: I am only providing the bare minimums needed here… You would, of course, also want to include additional fields outside of those you are pulling from the User Profile Database)

(Further Important Note: I have used one Text box for the Consultant’s name E-mail and Phone are just going to be Expression Box fields added later.  There are several reasons for this odd configuration: 1) Email and Phone are pulled from the user’s info and as such have no need to be user editable. 2) However, Consultant which is also auto populated (and therefore should also not need editing; in fact you would likely format this field to be read only for this reason) IS likely going to be needed to perform other lookups elsewhere on the form (i.e. possibly pulling  back projects that the consultant has been assigned to work on so that when they use the Project Drop down list to add a line item they will only see projects that they are working on in the list).  In order to perform this kind of lookup you cannot use an expression box.)

Next double click on each of the two fields and set the:
Field Name of the Text field to Consultant
Field Name of the Date Field to Date (also set the date field Data Type to Date and then in Default Value click on the fx button and Insert the Today function and click OK 2x).  This will automatically add the current day’s date to the form when filling it out for the first time.

Basic form named

Basic Form Date Format

 

Now we are ready to add the connection to the SharePoint Web Service…

In InfoPath, navigate to the Tools menu > Data Connections…. > Add… (Now add the following values to this Add… page)
Create a new connection to: Receive data > Click Next > Web Service > Next >
Type in the location of the web service using the Top Level Site in your site collection
(i.e. http://(topLevelSiteUrl)/_vti_bin/UserProfileService.asmx) and click Next
If you typed the address in correctly, InfoPath will connect to the data source and you should choose GetUserProfileByName
In the Select an Operation Section and click Next > Next > Next >
Make sure you select the Automatically receive data when the form is opened is selected and click Finish > Close
Our connection to the User Profile Database is now ready to use!

(Note: for more information on the 40 built in Web Services that MOSS exposes please see: Web Services Access)

Next let’s Populate the InfoPath Form using our new Data Connection

Populating the Consultant Text Box with the Current User’s Name

In InfoPath double click on the Consultant TextBox and in the Properties window that appears, Click the fx button (in the Default Value section) and click on Insert Field or Group…
Set the Data source: to GetUserProfileByName(Secondary), Expand the folders to find the Value field and click on the Filter Data… button and Add a new filter
LookupA
Now in the first column, choose Select a field or group… and select Name from the GetUserProfileByName(Secondary) data source as shown below and click OK
LookupB

In the 2nd column ensure that the value is set to is equal to
In the 3rd column select Type Text… and type AccountName
(SUPER IMPORTANT:
The Spelling of AccountName is INCREDIBLY IMPORTANT AND INCREDIBLY CASE SENSITIVE…. Unless you want it NOT to work…)
Click OK 5 times to return to the InfoPath Form.

(Note: You may also be able to use PreferredName assuming that this field is Populated in Active Directory but PreferredName is not guaranteed to be unique like AccountName is which is why I chose AccountName  in this example)
(Also Note: the origin of these Internal names from the user profile database (e.g. AccountName) may be found if you Open the MOSS SSP (i.e. Open Central Administration > SSP (default name SharedServices1) > User Profiles and Properties > View Profile Properties > Pick the property you are interested in and Edit  then note the internal Name property as shown in the image below)
UserProfileInternalNames

 

 

Populating the Current User’s E-mail Address

In InfoPath from the Controls task pane (Insert menu > More Controls…) Click and drag the Expression Box control to the table cell just to the right of the Email: label.
In the Insert Expression Box window, click fx button (in the Default Value section) and click on Insert Field or Group…
Set the Data source: to GetUserProfileByName(Secondary), Expand the folders to find the Value field and click on the Filter Data… button and Add a new filter
LookupA
Now in the first column, choose Select a field or group… and select Name from the GetUserProfileByName(Secondary) data source as shown below and click OK
LookupB

In the 2nd column ensure that the value is set to is equal to
In the 3rd column select Type Text… and type WorkEmail
(SUPER IMPORTANT:
The Spelling of WorkEmail is INCREDIBLY IMPORTANT AND INCREDIBLY CASE SENSITIVE…. Unless you want it NOT to work…)
Click OK 5 times to return to the InfoPath Form.

Populating the Current User’s Phone Number

In InfoPath from the Controls task pane (Insert menu > More Controls…) Click and drag the Expression Box control to the table cell just to the right of the Phone: label.
In the Insert Expression Box window, click fx button (in the Default Value section) and click on Insert Field or Group…
Set the Data source: to GetUserProfileByName(Secondary), Expand the folders to find the Value field and click on the Filter Data… button and Add a new filter
LookupA
Now in the first column, choose Select a field or group… and select Name from the GetUserProfileByName(Secondary) data source as shown below and click OK
LookupB

In the 2nd column ensure that the value is set to is equal to
In the 3rd column select Type Text… and type WorkPhone
(SUPER IMPORTANT:
The Spelling of WorkPhone is INCREDIBLY IMPORTANT AND INCREDIBLY CASE SENSITIVE…. Unless you want it NOT to work…)
Click OK 5 times to return to the InfoPath Form.

 

 

Try the Form out to see the Auto-Population take place…

If you did everything right you should now be able to go to the InfoPath toolbar and click on Preview… (Note: you may have to click Yes to a security warning)  (Further Note: This assumes that the user running InfoPath has a user profile in SharePoint).

It is just that easy!

Enjoy!

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Comments are closed