http://www.developer.com/

Back to article

Dynamically Data-bind in InfoPath


September 24, 2003

In Part 1 of this article series, we looked at some of the powerful and compelling features of InfoPath 2003. In this installment, we will see how to create a dynamic data-bound InfoPath from an ASP.NET Web Service. We will also see how to customize the task pane by adding custom content that is relevant to the form that the user is filling out.

Creating a Data-Bound InfoPath Using an ASP.NET Web Service as the Data Source

In this section, we will understand how to construct a dynamic, data-bound InfoPath form using an ASP.NET Web Service as the data source. Before we move on to consuming a Web Service from an InfoPath form, let us create an ASP.NET Web Service, which can then be used from within an InfoPath form.

Creating an ASP.NET Web Service

Let us start by creating a Web Service named InfoPathWebServiceExample by selecting Visual C# ASP.NET Web Service as the project template in the New Project dialog box. Once the project is created, rename the default service from Service1 to AuthorsService. To the AuthorsService, add the following two methods:

[WebMethod]
public XmlDocument GetAuthorByAuthorID(string authorID)
{
  XmlDocument doc = new XmlDocument();
  DataSet authorsDataSet = new DataSet("Authors");
  using (SqlConnection conn = new
SqlConnection("server=localhost;uid=sa;pwd=thiru;database=Pubs"))
  {
    SqlDataAdapter adapter = new SqlDataAdapter("Select au_id,
                             au_lname, au_fname,
                             phone from authors
                             Where au_id = '" +
                             authorID + "'" ,conn);
    adapter.Fill(authorsDataSet,"Author");
  }
  doc.LoadXml(authorsDataSet.GetXml());
  return doc;
}

[WebMethod]
public XmlDocument GetAllAuthors()
{
  XmlDocument doc = new XmlDocument();
  DataSet authorsDataSet = new DataSet("Authors");
  using (SqlConnection conn = new
         SqlConnection("server=localhost;uid=sa;pwd=thiru;
                        database=Pubs"))
  {
    SqlDataAdapter adapter = new SqlDataAdapter("Select au_id,
                             au_lname, au_fname,
                             phone from authors " ,conn);
adapter.Fill(authorsDataSet,"Author");
  }
  doc.LoadXml(authorsDataSet.GetXml());
  return doc;
}

The above methods are very similar except for the difference that the GetAuthorByAuthorID method takes an authorID as an argument and returns information about that particular author. The GetAllAuthors method returns all the authors present in the authors table in the pubs database. If you navigate to the AuthorsService.asmx file from the browser and invoke the GetAllAuthors method using the default test harness provided by ASP.NET, you will get the following output.



Click here for a larger image.

Creating the InfoPath form

Now that we have constructed the Web Service, let us create the InfoPath form. To create the form, go through the following steps.

  1. Start by selecting File->Design a Form from the menu.
  2. In the Design a new form task pane, click on New from Data Source.
  3. Select Web Service as the type of the data source in the first screen of the Data Source Setup Wizard. Clicking on Next in the first screen results in the following screen.
  4. In the above screen, you must indicate how the form will interact with the Web Service, indicating options such as whether it receives and submits data to the service. You can build a form that only sends data, one that only receives data, or one that does both. If you choose Receive and submit data in the above screen, InfoPath will create two views for your form: one view for submitting data to the service and another view for the data returned from the service. In this case, the data source view also contains two groups of fields. The queryFields contain the data that needs to be supplied when invoking the service. The dataFields contain the data returned from the Web Service. For the purposes of this article, let us select Receive data option in the above screen. Clicking Next on the above screen results in the following screen, where you need to specify the location of your Web Service.
  5. In the above screen, enter the location of the Web Service and click Next.
  6. From the location of the Web Service specified, InfoPath extracts the WSDL definition and displays the available Web Service methods.
  7. In the above screen, select the GetAllAuthors method from the list and click Next to complete the wizard.
  8. After that, open up the Data Entry View from the task pane and then click on Data Source.
  9. In the Data Source treeview, open up the dataFields node and select the Authors node. Right-click on the Authors node and select the Section with Controls option from the context menu.
  10. Now, navigate back to the Query view and then click on the Preview Form option from the toolbar.
  11. In the Preview mode, if you click on the Run Query button, you will see the following output being displayed.


  12. Click here for a larger image.

    The above output is retrieved from the GetAllAuthors Web Service method call that returns details about all the authors.

Customizing the Task Pane to Display Custom Content

In this section, we will see how to add custom content to the task pane that will be displayed to the user at the time of filling out the form. By providing meaningful content, we can provide the users with helpful information they need while filling out the forms. For the purposes of this article, let us display all the publishers available in the Publishers table in the custom task pane.

For this reason, let us create a new Visual C# ASP.NET project named TaskPaneCustomContent. Once the project is created, add a Web Form named PublishersList.aspx to the project. After that, add a DataGrid control named gridPublishers to the form and then modify the code in the Page_Load event to look like the following.

private void Page_Load(object sender,
                       System.EventArgs e)
{
  DataSet publishersDataSet = 
         new DataSet("Publishers");
  using (SqlConnection conn = new
         SqlConnection("server=localhost;uid=sa;
                        pwd=thiru;database=Pubs"))
  {
    SqlDataAdapter adapter = 
         new SqlDataAdapter("Select pub_name,
             country from Publishers ",conn);
    adapter.Fill(publishersDataSet ,"Publisher");
  }
  gridPublishers.DataSource =
      publishersDataSet.Tables[0].DefaultView;
  gridPublishers.DataBind();
}

Now that we have created the ASP.NET page that is going to provide content to the task pane, let us see how to hook it up with the InfoPath form. To accomplish this, select Tools-> Forms Options from the menu and select the Advanced tab from the Forms Options dialog box. In the dialog box, check the Enable custom task pane check box, and enter a custom name as well as the location of the ASP.NET page that we created earlier. After entering all the details, your dialog box should look like the following screenshot.

Now, if you preview the form by clicking on the Preview form option from the toolbar, you will get the following output.



Click here for a larger image.

As the above figure shows, the output produced by the PublishersList.aspx page is displayed in the Custom task pane of the form.

Conclusion

In this article series, we have understood the key features of InfoPath 2003 that make it an ideal candidate for creating productivity applications using the familiar Office suite of tools. InfoPath has the ability to become the widely used data capturing tool, because of its native ability to create and consume XML data, allowing the data in an enterprise to be reused without writing any custom code. You can achieve all of this without requiring the users to know XML or possess development skills, but still leverage the features of XML throughout the enterprise. Also by using InfoPath, you can simply develop Web Services "hooks" that allow data to be submitted to and retrieved from a variety of applications. Developers can use InfoPath to quickly create new, feature-rich user interfaces to those legacy applications. This process is both much faster and less expensive than the task of re-engineering the legacy application, and can result in a huge cost saving.

Source Code

The following is the source code for this two part article: InfoPath.zip - 52 kb.

About the Author

Thiru Thangarathinam has many years of experience in architecting, designing, developing and implementing applications using Object Oriented Application development methodologies. He also possesses a thorough understanding of software life cycle (design, development and testing). He is an expert with ASP.NET, .NET Framework, Visual C#.NET, Visual Basic.NET, ADO.NET, XML Web Services and .NET Remoting and holds MCAD for .NET, MCSD and MCP certifications. Thiru has authored numerous books and articles. He can be reached at thiruthangarathinam@yahoo.com.

# # #

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date