With InfoPath 2007 there is a new function introduced which allows us to get the current username without any .NET coding. Simply use the username() default value box to fill a text box with the username:
The good thing is: it's very easy to use without any coding and works in InfoPath 2007 as well as in SharePoint Forms Server solutions. But the bad think is: only the username can be used - no information about email (e.g. for Workflows), manager, phone, fax information. But there are two ways to overcome that limitation:
let's start with 1):
Another way is to use the OOB MOSS 2007 Web Service. MOSS 2007 comes with a nice technology called User Profile Store (UPS) which "extends" Active Directory with the User Metadata for SharePoint. The nice thing is that you can sync the UPS with the Active Directory. You can find the Mapping Definition in the SharePoint Central Administration at "Shared Services->User Profile and Properties->View Profile Properties" As soon as we imported the AD Data into the USP, we can access the User data via the MOSS Web Services. The Web Service for the UPS can be found at http://[Servername]/_vti_bin//UserProfileService.asmx.
So - let's get started to use the web service from InfoPath. First start with a new blank form template. Add a blank text box to the form and name it "Username". Then add the Web Service to the UPS to the secondary Data Sources (Tools->Data Connections->Add) as Receiving Data Connection to a Web Service as http://[Servername]/_vti_bin//UserProfileService.asmx?WSDL After that you get a list of the available Web Methods - the Method GetUserProfileByName is the one which helps us: as you can see the Web Service only expects one parameter - surprise, surprise - the Account Name of the user we are looking for. (in the form DOMAIN\AccountName)
On the last page uncheck the "Automatically retrieve data ..." box, because we want to call the web service manually.
That's all ! All we have to do is to supply the web service we registered before with a parameter (Account Name), call the web service and display the result.
The best place to call the web service are the Rules of the Textbox (Username) - they are called whenever the value of the textbox changes. To modify them double-click on the textbox and the property page opens - here you can find the rules button:
Add a new rule which consists of two actions - the first sets the parameter of the web service and the second calls the web service. The magic part is that you don't set a field of the Main Data Source - you must set the query fields of the secondary (=web service) Datasource.
The value we want to use is - of course - the textbox:
After setting the query value of the web service - call it !
The last thing we must do - is to display the data. The best control for that is a Repeating Table. So add a Table Control to the form (change the number of columns to 2)
Now we change the binding of the table to the result of the secondary datasource (=Web Service).
And now - let's test it:
The warning Page comes from InfoPath whenever you access an "untrusted" data source - you can avoid this by signing and trusting the form (http://msdn2.microsoft.com/en-us/library/Bb251020.aspx)
In a view days i will show you how you can use a custom web service to access the same data direct from the Active Directory