1. Create a Namespace Variable
Scenario/Problem: | You need to create a namespace variable. |
|
Solution: | Declare a string variable and set that to the namespace using the NamespaceManager object. |
Coding with InfoPath involves using XML/XPath
objects and methods as well as XPath expressions. A key element in
using the XML/XPath methods is the namespace that is used for the XML
data retrieval. The NamespaceManager object enables you to look up the namespace. By default, the namespace in InfoPath data is my.
Because the namespace is used throughout various
methods in this solution, the first line of code in the changed method
is a string variable declaration, as shown in Listing 1.
Listing 1. Namespace Declaration
//Namespace variable
string myNamespace = NamespaceManager.LookupNamespace("my");
2. Access the Secondary Data Source
Scenario/Problem: | You need to access the secondary data source. |
|
Solution: | Create DataSource and XPath objects using the secondary data source information. |
The next step in the overall solution is to declare
objects based on the secondary data source. You need the name of the
secondary data source and the XPath of the rows that are returned. To
retrieve the XPath of the rows returned, follow these steps:
1. | Switch over to your InfoPath form.
|
2. | In the Fields pane, switch the data source to the secondary data source. (For this example, it is Offices.)
|
3. | Expand the datafields folder.
|
4. | Right-click
the repeating group under the datafields folder and select Copy XPath
to copy the XPath string to the Clipboard, as shown in Figure 1.
|
5. | Switch back to the code-behind and paste the XPath into the code as needed.
|
The secondary data source setup code is shown in Listing 2. Paste the XPath from the previous steps into the XPathNavigator Select method. Place this block of code after the namespace variable.
Listing 2. Secondary Data Source Setup
//Secondary data source setup
DataSource ds = DataSources["OFFICES"];
XPathNavigator domNav = ds.CreateNavigator();
XPathNodeIterator rows = domNav.Select
("/dfs:myFields/dfs:dataFields/d:SharePointListItem_RW",
NamespaceManager);