Although you can easily get hold of the logged in users login name with
this.Application.User.LoginName
it's doesn't seem to be so easy to get hold of the users full name. It is however releatively easy to accomplish by making a call to one of the many webservices provided by MOSS, specifically the GetUserInfo method of the UserGroup webservice, which I'll guide you through now.
You'll need to add the following class that I've created using the xsd.exe tool (it represents the details returned in the User node of the GetUserInfo Method) to your project.
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
// Runtime Version:2.0.50727.42
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=2.0.50727.42.
//
/// [System.CodeDom.Compiler.
GeneratedCodeAttribute(
"xsd",
"2.0.50727.42")]
[System.
SerializableAttribute()]
[System.Diagnostics.
DebuggerStepThroughAttribute()]
[System.ComponentModel.
DesignerCategoryAttribute(
"code")]
[System.Xml.Serialization.
XmlTypeAttribute(AnonymousType=
true)]
[System.Xml.Serialization.
XmlRootAttribute(Namespace =
"http://schemas.microsoft.com/sharepoint/soap/directory/", IsNullable =
false)]
public partial class User {
private string idField;
private string sidField;
private string nameField;
private string loginNameField;
private string emailField;
private string notesField;
private string isSiteAdminField;
private string isDomainGroupField;
/// [System.Xml.Serialization.
XmlAttributeAttribute()]
public string ID {
get {
return this.idField;
}
set {
this.idField =
value;
}
}
/// [System.Xml.Serialization.
XmlAttributeAttribute()]
public string Sid {
get {
return this.sidField;
}
set {
this.sidField =
value;
}
}
/// [System.Xml.Serialization.
XmlAttributeAttribute()]
public string Name {
get {
return this.nameField;
}
set {
this.nameField =
value;
}
}
/// [System.Xml.Serialization.
XmlAttributeAttribute()]
public string LoginName {
get {
return this.loginNameField;
}
set {
this.loginNameField =
value;
}
}
/// [System.Xml.Serialization.
XmlAttributeAttribute()]
public string Email {
get {
return this.emailField;
}
set {
this.emailField =
value;
}
}
/// [System.Xml.Serialization.
XmlAttributeAttribute()]
public string Notes {
get {
return this.notesField;
}
set {
this.notesField =
value;
}
}
/// [System.Xml.Serialization.
XmlAttributeAttribute()]
public string IsSiteAdmin {
get {
return this.isSiteAdminField;
}
set {
this.isSiteAdminField =
value;
}
}
/// [System.Xml.Serialization.
XmlAttributeAttribute()]
public string IsDomainGroup {
get {
return this.isDomainGroupField;
}
set {
this.isDomainGroupField =
value;
}
}
}
. . .
You'll also need to set the forms security level to Full Trust.
Now we need to create the data connection to the web service.
Click Data -> Data Connections
Click Add
Select Create a new connection to Receive data then click Next
Select Web service then click Next
Enter http://YourServerName/_vti_bin/UserGroup.asmx for the webservice location making sure you put in your moss servers name then click next
Select GetUserInfo from the list of available operations then click Next
Infopath needs to get more info from the webservice operation which means you'll have to provide a valid Login name for it to query the webservice with, so click on the Set Sample Value button & input a user in the following format Domain\Login
Click OK then click Next
Click Next
Click Next
Enter a meaningful name for the data connection & ensure you remove the tick from Automatically retrieve data when the form is opened. Then click Finish & close the data connections window. Within your form code you can now add the using directive to System.Xml.Serialization & the following to the formloading event to retrieve the UserInfo details
public void InternalStartup()
{
this.EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
}
void FormEvents_Loading(object sender, LoadingEventArgs e)
{
//get hold of the datasource for the webservice operation
DataSource ds = this.DataSources["GetUserInfo"];
//Create an XPathNavigator to the node we have to set to query the method
XPathNavigator userField = ds.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:queryFields/tns:GetUserInfo/tns:userLoginName", this.NamespaceManager);
//Set the query value to the current users login name
userField.SetValue(this.Application.User.LoginName);
//execute the webservice operation
ds.QueryConnection.Execute();
//the data will now be in the datasource
XPathNavigator userInfo = this.DataSources["GetUserInfo"].CreateNavigator();
//add the namespaces so we can query the datasource
XmlNamespaceManager umanager = new XmlNamespaceManager(userInfo.NameTable);
umanager.AddNamespace("dfs", "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution");
umanager.AddNamespace("tns", "http://schemas.microsoft.com/sharepoint/soap/directory/");
//get hold of the node we're after
XPathNavigator usernode = userInfo.SelectSingleNode("/dfs:myFields/dfs:dataFields/tns:GetUserInfoResponse/tns:GetUserInfoResult/tns:GetUserInfo/tns:User", umanager);
//deserialize the contents of the node into our custom class
User user;
XmlSerializer serializer = new XmlSerializer(typeof(User));
user = (User)serializer.Deserialize(usernode.ReadSubtree());
//set the value of a field within out main data source i.e. the InfoPath form
//to the users full name
XPathNavigator field = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:field1", this.NamespaceManager);
field.SetValue(user.Name);
}
. . .
and voila!
If you take a look at the User class you'll notice that there's other information available to you from the GetUserInfo method.
Hope this information is useful to someone & saves them some time trying to figure this one out.