AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=498&pId=-1
Secondary Data Source Web Service Parameters using C# at Runtime in InfoPath
page
by Michelle Beall
Feedback
Average Rating: 
Views (Total / Last 10 Days): 24024/ 36

Introduction

When creating forms one thing users have become accustomed to is having dynamic forms with features such as cascading drop down lists.  Microsoft’s latest addition to the Office suite, InfoPath, provides the means to swiftly create structured data forms and with a little code some of these dynamic capabilities can also be implemented.

 

I tried to replicate some of the functionality I was able to provide in ASP.NET forms using Microsoft InfoPath SP1.  Since it is still a new product I found resources to be scarce, so I hope this article will help others adopt and use InfoPath meaningfully in a shorter time.

 

One of the more useful techniques is to know how to pass parameters to a web service at runtime.  This technique lends itself well to creating dependent drop down lists and runtime lookups or queries. This example will demonstrate how to set an InfoPath form’s secondary data source web service parameter using managed code and uses the following:

  • Microsoft Office InfoPath 2003 Service Pack 1 (SP1) Preview
  • InfoPath 2003 Toolkit for Visual Studio .NET
  • Microsoft Visual Studio .NET 2003 (1.1 Framework)
  • Microsoft Visual C#
  • Web service running on IIS 6.0
Setting up the Example

In this example, assume you want to call a web service and pass in a single parameter supplied by the user at runtime.  You have a textbox on the form called wsParam1.  You also have a drop down list which is populated using a secondary data source.  You want to populate the drop down dynamically based on the entry made by the user in the textbox (or any other control for that matter) while the user is entering data into the form.

 

To start you create a new InfoPath project in Visual Studio.  In the InfoPath IDE add a text box and a drop down list with the following properties:

 

Text Box Name wsParam1
Data Type Text (string)
Drop-Down List Box Name field2
Data Type Text (string)
List box entries Lookup values in a data connection to the database, Web service, file, or SharePoint library or list

 

Click on the Add… button for the Drop-Down List Box Data Connection and follow the wizard.  In the final step ensure that the Automatically retrieve data when form is opened checkbox is unchecked.  Set the value and display name of the drop-down list box from the result set returned by the web service.

 

The stage is set and we’re ready to do some coding.  We now need to use an event handler to call the web service when a user enters data into the text box.  To access the event handler reopen the text box properties dialog.

 

Handling the OnAfterChange Event

In the text box properties dialog click on the Data Validation button to open the Data Validation dialog window.  In the Script section select OnAfterChange from the Events list and click on the Edit button.  The designer adds the following Event Handler method to FormCode.cs in Visual Studio:

// The following function handler is created by Microsoft Office InfoPath. Do not
// modify the type or number of arguments.
[InfoPathEventHandler(MatchPath="/my:myFields/my:wsParam1", 
    EventType=InfoPathEventType.OnAfterChange)]
public void wsParam1_OnAfterChange(DataDOMEvent e)
{
    // Write code here to restore the global state.

    if (e.IsUndoRedo)
    {
       // An undo or redo operation has occurred and the DOM is read-only.
       return;
    }

    // A field change has occurred and the DOM is writable. Write code here to respond
    // to the changes.

}

At the end of the Event Handler add a method call to RequeryWebService() method.  Then write the RequeryWebService() method.  This code sample assumes the web service has one parameter param1.

private void RequeryWebService()
{
    //Get a reference to the Web Service 
    IXMLDOMDocument3 wsDOM = 
       (IXMLDOMDocument3)thisXDocument.DataObjects["DataSourceName"].DOM;

    //Set the SelectionNamespaces so that you can find the correct field
    wsDOM.setProperty("SelectionNamespaces", 
       "xmlns:dfs=\"http://schemas.microsoft.com/office/infopath/2003/dataFormSolution\"" 
       + " xmlns:s0=\"http://tempuri.org/\"");

    //Set Web Service parameters
    wsDOM.selectSingleNode
       ("/dfs:myFields/dfs:queryFields/s0:DataSourceName/s0:param1").text =
       thisXDocument.DOM.selectSingleNode("/my:myFields/my:wsParam1").text;

    //Requery the webservice datasource
    thisXDocument.DataObjects["DataSourceName "].Query();
}

** Caution **
You will also want to add a rule via the property dialog to the drop down list (field2) to set the value to "" otherwise when the user has selected a value from the list and then enters different search criteria into the text box, the selected value remains in the drop down list.

 

Preview the form et voilà!  The user can enter a value into the text box and when the control loses focus the OnAfterChange event is fired, setting the web service parameter with the value from the text box, querying the web service and populating the drop-down list box with values returned.

 

This example be can easily extended to handle multiple web service parameters, and even to create cascading drop down lists.


 


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-20 1:21:45 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search