425-882-1032 info@3sharp.com

After reading my four-part series about the time-off request solution, someone asked if it were possible to retrieve user information into the form and thus save the end user from having to select an employee name from a drop-down list box control. Immediately, I thought of the declarative userName function in InfoPath 2007. This function returns the user name of the person filling out a form. Unfortunately, as I later found out from the InfoPath product team, this function is not supported in the Document Information Panel.

There is, however, an alternative to the declarative function. Within the Loading event for the form template that is used for the Document Information Panel, you could add managed code that calls the UserName property from the System.Environment class. This property provides the same functionality that you would get from the userName function. The following C# code sample shows how you can use this property to set the EmployeeName node in the form, which can now be bound to a text box control:

public void FormEvents_Loading(object sender, LoadingEventArgs e)
    if (MainDataSource.CreateNavigator().SelectSingleNode("//ns2:EmployeeName", NamespaceManager).InnerXml == string.Empty)
        MainDataSource.CreateNavigator().SelectSingleNode("//ns2:EmployeeName/@xsi:nil", NamespaceManager).SetValue("false");
        MainDataSource.CreateNavigator().SelectSingleNode("//ns2:EmployeeName", NamespaceManager).SetValue(System.Environment.UserName);

NOTE: Because the code is accessing settings on the current user’s computer during the Loading event, you will need to sign the form template with a code-signing certificate and set the security level to full trust before publishing it as a Document Information Panel for a content type.

Now this change by itself might not be enough for the time-off request solution. If you recall from my previous posts, the solution uses a Time-Off Balances custom list to store vacation balances of employees. The Document Information Panel has a data connection that retrieves information from that custom list. So, you could add a username column to that list and then include a declarative rule for the EmployeeName field, similar to the one created for the original drop-down list box control. You might even want to update the content type to include username columns for both the employee and manager.

In any event, retrieving user information with the Document Information Panel is possible. It just cannot be done with the userName function.