Hey Linus,
first install the InfoPath toolkit for Visual Studio that you can use managed code for you InfoPath projects. Then put this into the OnLoad event of your form to get the currently logged on users name form AD.
NOTE: This requires to set your form to full trust level.
If you don't want to do so use the code with a WebService.
IXMLDOMNode name = thisXDocument.DOM.selectSingleNode("my:myFields/my:name");
try
{
string user = System.Environment.UserName;
// Create a directory searcher on the path provided when created the instance
DirectoryEntry entry = new DirectoryEntry("LDAP://OU= ,DC=,DC=,DC=");
DirectorySearcher searcher = new DirectorySearcher(entry);
string ToFind = "(SAMAccountName=" + user + ")";
searcher.Filter = ToFind;
// Try to find the employee
SearchResult result = searcher.FindOne();
if (result != null)
{
// Get the 'DirectoryEntry' that corresponds to 'result'.
DirectoryEntry myDirectoryEntry = result.GetDirectoryEntry();
// Get the properties of the 'mySearchResult'.
ResultPropertyCollection myResultPropColl;
myResultPropColl = result.Properties;
foreach(Object myCollection in myResultPropColl["name"])
{
PE.text = myCollection.ToString();
}
name.text = "Name Set";
}
}
catch (Exception ex)
{
ex.Message.ToString();
}