Customizing Web Service Interaction Through Managed Code
One of the most used data connections is a Web Service, and often times the default Web Service connection behavior does not offer the granular level of control that is desired. Since the new InfoPath Object Model is handled through managed code out of the System.Xml namespace, custom web service code can easily be written in order to handle web service consumption on a more granular scale. As well, this custom .NET code can be debugged by attaching to the appropriate w3wp process, so breakpoints can be set throughout the custom Web Service code, or any .NET code that is being implemented with InfoPath for that matter.
To setup a new method in order to handle the consumption of a Web Service it is required to firstly make the System.Xml reference within the new class file. Following, it is necessary to use an XPathNavigator in order to provide a cursor into the XML node set by calling the CreateNavigator method. The DataSource argument that is passed in is web service datasource name. This is opposed to use the MainDataSource property that was used in previous example which would set the data source as the main data source of the InfoPath form.
- XPathNavigator nav = this.DataSources["SharePointPro2007DataSource"].CreateNavigator();
Setting Web Service Parameters
Next, you must send the Web Service an input parameter must .This can be done in one of two ways, either by setting the value directly when using the SelectSingleNode method or by using the XPathNavigator in order to assign a variable that will house the results of the SelectSingleNode method which can then have the value cast to it. Both will achieve the same result.
- nav.SelectSingleNode("//dfs:myfields/tns: SharePointPro2007/tns:pro2007name",
- // Store and Set Value XPathNavigator sharepointPro2007Nav = nav.SelectSingleNode
- "dfs:myfields/tns: SharePointPro2007/tns:pro2007name ", this.NamespaceManager); sharepointPro2007Nav.SetValue("prosp2007Value");
In order to retrieve the values from the web service, you need to retrieve the Web Service by using an Execute() method. Similar to the above scenario in regards to setting a parameter, the connection can either firstly be assigned to a variable or called directly.
- // Direct Execution Method DataSources["SharePointPro2007DataSource"].QueryConnection.Execute();
- //Store and Execute Method DataConnection proSP2007connection =
- this.DataConnections["SharePointPro2007DataSource"]; proSP2007connection.Execute();
Using either method results in the queried value being returned as a string, which can further manipulated through supplementary code if desired. Once the query is setup, the result from the Web Service query can be returned by casting the values to a string using a SelectSingleNode method and capturing the Value.