Type the following code in the body of the stub method:
string xpath=@"/my:EquipmentRequest/my:Customer/my:CompanyName";
XPathNavigator nav = this.MainDataSource.CreateNavigator();
string companyName = nav.SelectSingleNode(xpath,this.NamespaceManager).Value;
DataSource customer = this.DataSources["Customer"];
AdoQueryConnection cnn = customer.QueryConnection as AdoQueryConnection;
string allItemsQuery = cnn.Command;
cnn.Command =allItemsQuery + " Where C.CompanyName like '%" + companyName + "%'";
cnn.Execute();
cnn.Command = allItemsQuery;
A few items in this code sample require some explanation—first, the xpath
variable: As mentioned earlier, InfoPath is an XML-based form designer.
On the surface, this may seem trivial, since all developers are
familiar with XML and the various objects in the .NET Framework that
can be used to work with XML. However, because InfoPath is completely
XML-based, things work a bit differently and it can take time to fully
understand the difference.
In traditional
WinForms or ASP.NET programming, the presentation user interface is
separate from the data. Often, most of the code that we write involves
either reading data from or writing data to controls. Since the
presentation layer in InfoPath is simply a transformation of the data
(that is, an XSLT transform of XML data), there is no presentation
layer to manipulate programmatically. Data is automatically bound to
the appropriate controls. If we were writing an ASP.NET application, we
may retrieve the value of the CompanyName text box by examining the
Text property of the appropriate control; in InfoPath, there is no such
control and we retrieve the value of the CompanyName field by querying
the data model directly. The most common way to perform this query is
to use XPath, and the xpath variable stores the XPath expression that refers to the appropriate field.
Tip
InfoPath Designer makes it easy to determine the
XPath expression for a particular field. In the Fields pane,
right-click the required field and select Copy XPath from the context
menu. The XPath expression will be placed on the clipboard ready for
use in custom code.
The next item that warrants some
explanation is the DataSource object. The following class diagram shows
how data sources are defined by the InfoPath object model:
You’ll
notice that all of the classes in this diagram are abstract. All data
connections are ultimately defined using XML schemas and other
XML-based configuration data. At runtime, InfoPath creates objects from
these files that derive from the appropriate base class. For example,
in our code sample, we declare an object of type AdoQueryConnection to
allow us to modify the query for our database connection. In reality,
the actual type of this object is AdoQueryConnectionHost, which is an
internal type that is created when the connection details are
deserialized.
Note
Not
all InfoPath forms can make use of managed code due to security
restrictions in SharePoint. Custom list template forms and workflow
forms that are automatically generated using SharePoint Designer can’t
use managed code. When managed code is not available for a particular
form type, the Developer tab will not appear in the ribbon.
Now that our customer search function works properly, we can publish the form to SharePoint and check out the results.