InfoPath 2010 Custom Code–Altering a Field

 

Here’s a simple task – alter a field in the code behind for an InfoPath 2010 Form. Not as easy as you’d hope. You have to dust off your XML skills get this working.  Here’s some sample code:

 

 XPathNavigator fieldTwo = MainDataSource.CreateNavigator().SelectSingleNode(“/my:myFields/my:field2”,NamespaceManager);

Make sure to add the NamespaceManager parameter… otherwise, you’ll get the error “Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function” error. Of course, you’ll only see that descriptive error if you’re running this through a debugger, or have trapped + logged the error in a useful manner. More likely, you’ll see an Event in the SharePoint “Operational” Windows Log indicating an unhandled exception, of the form “Business logic failed due to an exception”