How to: Log Values to a Field for Debugging
Applies to: InfoPath 2010 | InfoPath Forms Services | Office 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio Tools for Microsoft Office
When debugging an InfoPath form template, it is often useful to log values directly into a field in the form to create a record of debug data during a session of testing the form. The following procedures show how to create a multi-line field, and then add helper functions to the form code that enable you log debug data into that field.
To create a multi-line text field
Add a Text Box control to the form, and then resize it so that it can display multiple lines.
Right-click the text box, click Text Box Properties, and then click the Multi-line check box on the Display tab.
To add helper functions to log debug information to the field
On the Developer tab, click Code Editor, and then save the form template if you are prompted.
In the Code Editor, add the following three helper functions to the public class in the form code file.
Important
Make sure that you update the value set for the debugFieldXpath variable in the AddToDebugField function to the correct XPath expression for the field bound to the control that you created in the first procedure.
private void AddToDebugField(string valueToAdd) { // Update the value of debugFieldXpath to the XPath of the // multi-line field where you want to log debug information. string debugFieldXpath = "/my:myFields/my:field1"; string headerLine = "----------------- " + DateTime.Now + " -----------------" + "\r\n"; SetDebugFieldValue(debugFieldXpath, headerLine + valueToAdd + "\r\n" + GetDebugFieldValue(debugFieldXpath)); } private string GetDebugFieldValue(string xpath) { return this.CreateNavigator().SelectSingleNode(xpath, this.NamespaceManager).Value; } private void SetDebugFieldValue(string xpath, string value) { this.CreateNavigator().SelectSingleNode(xpath, this.NamespaceManager).SetValue(value); }
Private Sub AddToDebugField(ByVal valueToAdd As String) ' Update the value of debugFieldXpath to the XPath of the ' multi-line field where you want to log debug information. Dim debugFieldXpath As String = "/my:myFields/my:field1" Dim headerLine As String = "----------------- " _ & DateTime.Now & " -----------------" & vbCrLf SetDebugFieldValue(debugFieldXpath, (headerLine & valueToAdd & vbCrLf) _ & GetDebugFieldValue(debugFieldXpath)) End Sub Private Function GetDebugFieldValue(ByVal xpath As String) As String Return Me.CreateNavigator().SelectSingleNode(xpath, _ Me.NamespaceManager).Value End Function Private Sub SetDebugFieldValue(ByVal xpath As String, ByVal value As String) Me.CreateNavigator().SelectSingleNode(xpath, _ Me.NamespaceManager).SetValue(value) End Sub
Visual Basic Note When using Visual Basic, add Imports Microsoft.VisualBasic.Constants to the directives at the top of the form code file.
To test the AddToDebugField function
On the Developer tab, click Loading Event, and then add the following line of code to the event handler.
AddToDebugField("Form loaded");
AddToDebugField("Form loaded")
On the Developer tab, click View Switched Event, and then add the following line of code to the event handler.
AddToDebugField("View switched: " + this.CurrentView.ViewInfo.Name);
AddToDebugField("View switched: " & Me.CurrentView.ViewInfo.Name)
On the Home tab, click Preview.
The debug field should display two entries: one indicating that the form is loaded, and another indicating the name of the view. These examples use event handlers for events that occur as the form is opened. However, after the form is loaded, you can call the AddToDebugField function from other event handlers in addition to any other code running in the context of the form.