13.3. Developing InfoPath Projects in .NET

If you are a developer using .NET for other projects, you won't find much of a difference when working with the InfoPath form and your code. Before going into details on developing InfoPath forms in Visual Studio .NET, you need to make sure that you have two development tools: Visual Studio .NET 2003 and InfoPath 2003 Toolkit for Visual Studio .NET.

13.3.1. Review: Installing the InfoPath 2003 Toolkit for Visual Studio .NET

You should already have VS.NET 2003 installed by this point. The downloading and installing of InfoPath 2003 Toolkit for Visual Studio .NET has already been covered in Chapter 11, "Working with Code in Your InfoPath Form" in the section titled "Managed Code with .NET."

After downloading and installing the toolkit, you can open Visual Studio .NET. When you select a new project, additional choices will be displayed, offering C# or Visual Basic InfoPath .NET projects.

13.3.2. Working with the InfoPath Namespaces

When creating InfoPath forms using .NET, you will be using the InfoPath object model just as you do when using scripting. However, instead of accessing the object model directly, Microsoft has created namespaces to support the object model, creating the necessary classes with methods, properties, and events. The following table contains some of the more useful classes available in the Microsoft.Office.Interop.InfoPath.SemiTrust namespace:

ClassPurpose
DataObjectEnables developers to manipulate the data sources used in a form. There are DataObjects representing the different types of data adapters used, including: ADOAdapterObject, SharepointListAdapterObject, WebServiceAdapterObject, and the XMLFileAdapterObject object.
EmailAdapterObjectUsed to represent a data adapter that is used to generate an e-mail. This object is used in the next chapter using script.
ErrorObjectReturns errors from data validation.
XDocumentProvides information about a form and its underlying XML document. This object also contains the DOM object, allowing you to get the data.

You will probably spend most of the time using the XDocument object when developing using InfoPath and .NET. Using the XDocument class you just read about, you will now create an InfoPath project that uses code to update a form.

13.3.2.1. Try It Out: Creating an InfoPath .NET Project

You will create an InfoPath .Net project and utilize the XDocument object.

  1. Open Visual Studio .NET 2003.

  2. Click New Project.

  3. Click the Node in the Project Types tree view with the label Microsoft Office InfoPath Projects.

  4. Click Visual C# Projects.

  5. Locate and name the project as you wish, as displayed in Figure 13-10.

    Figure 13.10. Figure 13-10
  6. Click OK. The Microsoft Office Project Wizard opens, as shown Figure 13-11.

  7. Click the Finish button, keeping the default choice of creating a new form template. The new project is created, along with the various C# project files as well as a new form template.

  8. Click on the Controls task. The Controls task pane is displayed.

  9. Drag and drop a text box onto the form. By default the name of the new control on the form will be called field1, as shown in Figure 13-12.

  10. Choose Tools On Load Event to create the event routine for the Load event.

    Figure 13.11. Figure 13-11

    Figure 13.12. Figure 13-12
  11. Type the following lines of code in between the beginning and ending brackets.

    IXMLDOMNode Field1 = thisXDocument.DOM.selectSingleNode("//my:field1");
       if (thisXDocument.UI.Confirm("Store the word 'Test' in the field?",
                                     XdConfirmButtons.xdYesNo)==XdConfirmChoice .xdYes)
            Field1.text = "Test";

    The screen should then look as it does in Figure 13-13.

    Figure 13.13. Figure 13-13
  12. Click DebugRun to test the application. The code will then run, displaying the confirm box shown in Figure 13-14.

    Figure 13.14. Figure 13-14
  13. Click Yes to store the word test into the field on the screen. The confirm box closes, and test is placed in the field on the form, shown in Figure 13-15.

    There you have it, a simple InfoPath .NET application that updates a field on an InfoPath form. By running with this and expanding on the use of the object model using the InfoPath namespaces provided, you can create some pretty powerful applications. The next chapter shows you how to manipulate dates using C# and InfoPath. Now look at what you can do to distribute applications that you create.

    Figure 13.15. Figure 13-15
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.246.123