Tuesday, January 30, 2007

Querying Data Connections using the InfoPath 2007 Object model

Web services are completely integrated in InfoPath 2003/2007. We can submit and retrieve data whenever we want. If the current InfoPath functionalities does not satisfy your manager's ideas, you can improve your forms with custom code. Today I show you how to integrate web services by using the InfoPath 2007 Object Model

Actually, the way you used the Object Model in InfoPath 2003 has been changed since InfoPath 2007 has been released. The new Object Model is completely managed and is integrated with the .NET Framework 2.0 classes (System.Xml namespace)

Suppose we have a ASP .NET Web Service on server which defines the following web method:

[WebMethod]
public string SayHello(string name)
{
return "Hello " + name;
}

  • Now, Open your InfoPath Form Template and add a button on the screen
    (Design Tasks > Controls > Button)
  • Open the Button's properties by double-clicking on the button
  • Click on the 'Edit Form Code...' button to start up the Visual Studio Tools for Applications.
    (you can choose the programming language through the Form Options item in the Tools menu)
  • Add the following lines of code to your button handler:

    //do not forget to import the System.Xml namespace at the top of this class
    using System.Xml;

    //Create Xpath Navigator for WebService Data Source
    //"SayHello" = the name of the data connection

    XPathNavigator nav = this.DataSources["SayHello"].CreateNavigator();

    //Xpath expression to retrieve parameter xml node
    // tns:SayHello = method
    // tns:name = name of the argument at server side

    XPathNavigator webServiceNavi = nav.SelectSingleNode("dfs:myFields/dfs:queryFields/tns:SayHello/tns:name", this.NamespaceManager);

    //set parameter(s)
    webServiceNavi.SetValue("Kristof");

    //retrieve data: call the "SayHello" webservice
    DataConnection conn = this.DataConnections["SayHello"];
    conn.Execute();
  • The web method returns a string. You can drag and drop the dataFields section of your secondary data source (which corresponds to your web service connection) to the screen.

Attention

  • Using managed code in Browser Enabled Forms needs an administrator approval
  • In browser enabled forms you will mainly use web services to retrieve data
  • Have a special attention for the security issues
    - use Data Connection Files in Data Connection Libraries (UDCX)
    - InfoPath Forms Services makes the big picture more difficult, it introduces a extra tier.

    With InfoPath 2003, you never had to worry about security issues.
    You ran InfoPath on tier 1, and the Web service on tier 2. Now that you are designing forms that run in a web browser, you'll need to think about tier 3.


Tips

  • Did you know that you can use your 'Receiving Data Connections' to send data? The parameters specified at server side can be used to return a filtered set of data or as input for the database ...
  • Did you know that you can use your own .NET classes or Data Sets as parameter or as return value in web methods? Both are serializable to xml.

No comments: