1. Add the web service: http://[SharePointURL]/_vti_bin/UserProfileService.asmx
2. To get the current user:
using System.DirectoryServices;
string CurrentUser = System.Environment.UserName;
This will get the current SharePoint user, not the user currently logged into the computer.
3. Declare the InfoPath fields so you can write to them later:
XPathNavigator fieldAVPName = MyNavigator.SelectSingleNode("/my:myFields/my:Name", NamespaceManager);
4. Get the properties of the user
sharepoint.UserProfileService MyUsers = new sharepoint.UserProfileService();
MyUsers.Credentials = System.Net.CredentialCache.DefaultCredentials;
sharepoint.PropertyData[] propdata;
propdata = MyUsers.GetUserProfileByName(txtCurrentUser);
int NumProperties = propdata.Length;
string txtFirstName = null;
string txtLastName = null;
string txtName = null;
for (int i = 0; i < NumProperties; i++)
{
if (propdata[i].Name == "FirstName" && propdata[i].Values.Length > 0)
{
txtFirstName = (string)propdata[i].Values[0].Value;
}
else if (propdata[i].Name == "LastName" && propdata[i].Values.Length > 0)
{
txtLastName = (string)propdata[i].Values[0].Value;
}
else { }
}
txtName = txtFirstName + " " + txtLastName;
5. Write the data to InfoPath
fieldName.SetValue(txtName);
You can use this with minor modifications to get other AD Properties of the users.