Microsoft InfoPath 2010
The official blog of the Microsoft InfoPath team

Get the User Profile through MOSS Web Services

Get the User Profile through MOSS Web Services

Rate This
  • Comments 37

It is now easier than ever to pre-populate your InfoPath 2007 forms with the user profile information of the current user.


In Microsoft SharePoint Server 2003, the UserProfileService.GetUserProfileByName(string accountName) web service method required the caller to pass the account name (e.g., domain\alias) associated with the user profile to retrieve.  Though required, this argument was not needed when retrieving the profile of the current user.  After all, the form was running with the network credentials of the current user and those credentials were passed to the web service.

The typical work-around was to deploy a custom WhoAmI web service as a façade for the UserProfileService.  This façade would provide a method (e.g., WhoAmI() ) that would detect the user's identity and effectively forward a call to the UserProfileService, returning the eventual result to the caller.

New for Microsoft Office SharePoint Server 2007 and InfoPath 2007:

In InfoPath 2007, you can get the logon name of the current user by just using a new built-in function; but what if you need to get more data than just the username? Manager name, phone number, office location, and other data are all accessible through the UserProfileService: read on.

A form that uses the UserProfileService of MOSS to retrieve the User Profile of the current user no longer needs to explicitly pass the account information of the current user to the GetUserProfile method of the UserProfileService.  Simply passing a null value to this method will cause the web service to grab the user identity from the request.  The web service will then use that information as the account name and will subsequently return the User Profile of the current user.

Use this technique when you want to pre-populate the fields of an InfoPath 2007 form with user information obtained from the MOSS 2007 User Profile Database. 

The following instructions detail how to build a form to demonstrate this capability.


  • The default MOSS web application is configured to use Windows Authentication.
  • The MOSS server has an SSP with a User Profile Database that has been loaded from Active Directory
  • The end-user has a profile in the User Profile Database of the MOSS server
  • The end-user is logged into the client machine using his/her domain account.


Step 1: Create the form

  1. Open InfoPath and design a new, blank form
  2. Save the form as UserProfileDemo.xsn

Step 2: Design the form

  1. From the Layout Task Pane, add a 2 column x 4 row table to the form
  2. Place labels into the column 1 as follows: Name, Account, Email, and Phone
  3. Using the Controls Task Pane, drag four text box controls to your form, placing one in each row of column 2
  4. Using the Data Source Task Pane, rename the four "fieldX" data elements as follows: userName, userAccount, userEmail, and userPhone


Step 3: Set the security level of the form

  1. From the Main Toolbar, select Tools | Form Options
  2. Select Security and Trust from the list box
  3. Clear the checkbox next to Automatically determine security level
  4. Select Full Trust
  5. Hit OK.

Step 4: Add code to the form

  1. From the Main Toolbar, select Tools | Programming | Loading Event
  2. Right-click on UserProfileDemo node of the Project Explorer and choose Add Web Reference
  3. Enter the following for the URL: /_vti_bin/UserProfileService.asmx">http://<yourServerName>/_vti_bin/UserProfileService.asmx
  4. Enter the following for Web reference name: ProfileService
  5. Click Add Reference
  6. Replace the FormEvents_Loading event handler with the following code snippet:
public void FormEvents_Loading(object sender, LoadingEventArgs e) {
    XPathNavigator myRoot = MainDataSource.CreateNavigator();
    ProfileService.UserProfileService profileService = new ProfileService.UserProfileService();
    ProfileService.UseDefaultCredentials = true;
    ProfileService.PropertyData[] userProps = null;
    try {
        // Passing null to this method causes the profile of the current user to be returned.
        userProps = profileService.GetUserProfileByName(null);
    catch { }
    if (userProps == null || userProps.Length == 0) {
    for (int i = 0; i < userProps.Length; i++) {
        XPathNavigator node = null;
        switch (userProps[i].Name.ToLower())  {
            // these property names can be obtained by reviewing the user profile properties in the MOSS SSP.
            case "preferredname":
                node = myRoot.SelectSingleNode("/my:myFields/my:userName", NamespaceManager);
            case "accountname":
                node = myRoot.SelectSingleNode("/my:myFields/my:userAccount", NamespaceManager);
            case "workemail":
                node = myRoot.SelectSingleNode("/my:myFields/my:userEmail", NamespaceManager);
            case "workphone":
                node = myRoot.SelectSingleNode("/my:myFields/my:userPhone", NamespaceManager);
        ProfileService.ValueData[] values = userProps[i].Values;
        if (values.Length > 0) {
            if (node != null && string.IsNullOrEmpty(node.Value)) {


Step  5: Build and Preview

  1. Build the project and close VSTA.
  2. From the Main Toolbar, select File | Preview| Form
  3. When the form loads, the text boxes will be populated with your User Profile Information

This example opens the door to simplifying the form submission process for your users by reducing the time they take to complete forms, as well as ensuring the accuracy of the data that they enter.

Ronald Tielke
Microsoft Consulting Services
Desert Mountain District

Leave a Comment
  • Please add 6 and 2 and type the answer here:
  • Post
  • also i pass null to GetUserProfileByName(null), I get error: Error 2 Cannot implicitly convert type 'Form.ProfileService.PropertyData[]' to 'Form.ProfileService.ProfileService.PropertyData[]' P:\FormCode.cs 95 27 Form

  • FYI, for custom list forms in InfoPath 2010, the User Profile Service approach only works if anonymous access is disabled.  

    If you're allowing anonymous access, you will get an error message when the form loads saying InfoPath couldn't connect to a web service.

  • Pleaseeeeeeeeeeee help,

    I am having issue with calling web service from Infopath 2010 form (blank filer template).

    Error 2 Task failed because "sgen.exe" was not found, or the .NET Framework SDK v2.0 is not installed.  The task is looking for "sgen.exe" in the "bin" subdirectory beneath the location specified in the SDKInstallRootv2.0 value of the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework.  You may be able to solve the problem by doing one of the following:  1.) Install the .NET Framework SDK v2.0.  2.) Manually set the above registry key to the correct location.  3.) Pass the correct location into the "ToolPath" parameter of the task.

    Manualy I could run sgen.exe command and serialize dll, but from Infopath I am getting this error, whater I am running code from client or server.

    Do you have idea?

    Many thanks in advance

  • Hi Goran,

    To be honest, I am a bit confused: the only time I have seen this error is if you have added .NET code to your InfoPath form template. I have never seen this with a "blank" template. Did you just open the code editor and close it even? If so, this will add a code project to the XSN.

    At any rate, you can resolve this by installed the .NET 2.0 SDK which you can download from here:

    Now based on your comments, it does sound like you have code so I don't believe this is related to the UserProfileService. To confirm, create a new blank template and be sure to NOT add a code project. Add the data connection and just run it - it should work without issue.


  • I am new to Infopath i have created one form and published on SharePoint, now i want when somebody opend the form it username & email address of the logged on user populates into the form automatically.

  • Esta muy bien el código y funciona bien en vista previa del infopath pero cuando lo subo al servidor, ya no funciona no muestra nada al parecer no hace nada y la verdad ya no se que hacer llevo dos dias intentando resolver error me podrian ayudar gracias

  • I know this kind requests, are not the most common, however, I have not developed in any language since long time , and in addition to this, my infopath has only installed the VB language. Would someone kindly post the same piece of code but VB

Page 3 of 3 (37 items) 123