Share via


Products Secondary Data Source in the Calculations Developer Sample Form [InfoPath 2003 SDK Documentation]

Applies to:

Microsoft Office InfoPath 2003

For information on Service Pack 1 features for working with calculations, see the "Calculating Data" section in the InfoPath 2003 Help.

Secondary data sources allow you to retrieve information from other XML files, database connections, and Web services for use in your Microsoft Office InfoPath form. Secondary data sources can be used to provide the items in a list box or drop-down list box. You can write script to add data from a secondary data source to your form. To work with secondary data sources in your form, click Secondary Data Sources on the Tools menu in design mode.

The Secondary Data Sources dialog box allows you to modify an existing secondary data source or create a new one. To use an existing secondary data source, select the secondary data source you want in the Secondary data sources for your form list, and then click Modify. To create a new secondary data source, click Add. The Data Source Setup Wizard will then guide you through the required steps to modify an existing data source or to create a new data source.

Note  Secondary data sources in an InfoPath form do not appear in the Data Source task pane, and controls in a view cannot be bound to fields in a secondary data source.

The Calculations developer sample form uses the Products secondary data source to maintain the list of products entered and to populate the Product drop-down list box in Summary view. This data source is based on the Products.xsd XML Schema and has the following structure:

<Products>
   <Product>
      <id>1</id>
      <name>Product1</name>
   </Product>
<Product>

When a user adds or removes a product from the Products section in Products view, the InsertProduct and DeleteProduct custom functions update the product list in the secondary data source.

The InsertProduct custom function adds a product to the secondary data source when a user adds a product to the Calculations form in Products view. It accepts two arguments for the ID and name of the product to be added. The function uses the GetDOM method of the XDocument object to set a reference to the Products secondary data source, and then it uses the createNode method of the XML Document Object Model (DOM) to create a new XML node within the secondary data source. It then sets the values of the new node using the nodeTypedValue property and appendChild method of the XML DOM. The following is the JScript code for the InsertProduct custom function:

function InsertProduct(productID, productName)
{
   if (IsEmpty(productID))
      return;

   var productsAuxDom = XDocument.GetDOM("Products");
   var newProductNode = productsAuxDom.createNode(1, "product", "");
   var newIDNode = productsAuxDom.createNode(1, "id", "");
   var newNameNode = productsAuxDom.createNode(1, "name", "");
   var productsNode = productsAuxDom.selectSingleNode("/products");
   var oldProductNode = productsNode.selectSingleNode("product[id = '"
    + productID + "']");

   newIDNode.nodeTypedValue = productID;
   newNameNode.nodeTypedValue = productName;

   newProductNode.appendChild(newIDNode);
   newProductNode.appendChild(newNameNode);

   if (oldProductNode == null)
      productsNode.appendChild(newProductNode);
   else
      productsNode.replaceChild(newProductNode, oldProductNode);
}

Note  The IsEmpty function that is called at the beginning of the InsertProduct function is a custom function that is used to check the value of a variable. The code for the IsEmpty function can be found in the primary scripting file (script.js) for the Calculations developer sample form.

The DeleteProduct custom function deletes a product from the Products secondary data source when a user removes a product from the Calculations form in Products view. It accepts one argument for the ID of the product to be removed. The function uses the GetDOM method of the XDocument object to set a reference to the Products secondary data source, and then it uses the selectSingleNode method of the XML DOM to set a reference to product node to be removed. After the reference to the product node has been set, it uses the removeChild method of the XML DOM to remove the product node. The following is the JScript code for the DeleteProduct custom function:

function DeleteProduct(productID)
{
   if (IsEmpty(productID))
      return;

   var productsAuxDom = XDocument.GetDOM("Products");
   var productsNode = productsAuxDom.selectSingleNode("/products");
   var oldProductNode = productsNode.selectSingleNode("product[id = '"
    + productID + "']");

   if (oldProductNode != null)
      productsNode.removeChild(oldProductNode);
}