Using userName() in the Document Information Panel

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.

2 thoughts on “Using userName() in the Document Information Panel

  1. Hi,I have created a very simple custom document information Panel for a content type. When a new document is created and saved, the document gets saved correctly. But when I try to close the document I am asked “Do you want to save the changes” again.I have tried publishing the document with full trust, to a separate form template, directly to the content type but the problem persists.Any ideas?Thanks,NehaCode:public void DeleteNil(XPathNavigator node) { if (node.MoveToAttribute(“nil”, “http://www.w3.org/2001/XMLSchema-instance”)) node.DeleteSelf(); } public void FormEvents_Loading(object sender, LoadingEventArgs e) { // Write your code here. XPathNavigator root = this.CreateNavigator(); XPathNavigator test = root.SelectSingleNode(“//ns2:testcol1”, NamespaceManager); DeleteNil(test); test.SetValue(“abc”); }

  2. Hi, Neha.This is one of the consequences of using custom code in the Document Information Panel to update field values. The xsi:nil attributes are being updated in the data source, which results in an apparent “change”. You can try to set the form’s dirty property to “false” to see if that eliminates the “save” prompt…this.Dirty = false;I hope this helps…Regards,David

Leave a Reply

Your email address will not be published. Required fields are marked *