Monday, December 22, 2008

Access Field(s) from FormCode in an InfoPath Form

1. In the DataSource view, right click on the field you want to obtain the value from. Select, "Copy XPath" from the content menu.
2. In your FormCode utilize the following code (C#):

XPathNavigator myField = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:fieldName", NamespaceManager);

Where "myField" can be any variable name you'd like (I choose to call it the same as the name on the InfoPath form; and where "/my:myFields/my:fieldName" is the copied XPath from your clipboard (from Step 1).

You can now access that field like so:
string fooBar = fieldName.value

No comments: