Hosting the InfoPath 2007 Form Editing Environment in a Custom Web Form

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Summary: Learn how to use the XmlFormView ASP.NET control, together with InfoPath Forms Services, to create custom Web pages to render browser-enabled, interactive InfoPath forms. (21 printed pages)

Mike Talley, Microsoft Corporation

Namita Sheokand, Microsoft Corporation

Published: June 2006

Updated: April 2007

**Applies to:**Microsoft Office InfoPath 2007, Microsoft Office SharePoint Server 2007

Contents

  • Overview of the XmlFormView Control

  • Creating Custom Web Pages in SharePoint Server

  • Loading Browser-Enabled InfoPath Form Templates into the Control

  • Coding with the XmlFormView Control

  • Deploying Custom Web Pages to a Non-Root SharePoint Site

  • Considerations When Using the XmlFormView Control

  • Considerations When Using the XmlFormView Control

  • Conclusion

  • Additional Resources

Overview of the XmlFormView Control

A browser-enabled InfoPath form template is functionally equivalent to a form designed to run in Microsoft Office InfoPath 2007, but when it is published to and opened from a server running InfoPath Forms Services, it does not require Office InfoPath 2007 to be installed on the computer that uses the form. InfoPath Forms Services, as part of either Microsoft Office Forms Server 2007 or Microsoft Office SharePoint Server 2007, includes the XmlFormView ASP.NET control, which you can use to render a browser-enabled form in a custom Web page. The XmlFormView control benefits enterprises that want to incorporate the functionality of InfoPath forms into their organizational Web infrastructure.

Because InfoPath Forms Services is an extension to Windows SharePoint Services 3.0, the steps to open, configure, and add the XmlFormView control to a new Web page require common SharePoint Server development techniques, such as using the Internet Information Services (IIS) Manager to find the path of the SharePoint site, opening the site in Microsoft Visual Studio 2005, and configuring the Web configuration file for debugging.

This article describes the following tasks:

  • Creating a new Web page on a SharePoint server and adding the XmlFormView control to the page.

  • Creating and publishing a browser-enabled form template.

  • Writing code to control the XmlFormView control.

  • Deploying a custom Web page by using a SharePoint Server feature package.

Requirements

To complete the tasks in this article, you must have the following applications installed on your computer:

  • Microsoft Office InfoPath 2007

  • Microsoft Office SharePoint Server 2007 or Microsoft Office Forms Server 2007

  • Microsoft Visual Studio 2005

Creating Custom Web Pages in SharePoint Server

This section contains the following four tasks, which are required to create a custom Web page managed by Windows SharePoint Services.

  • Opening the SharePoint site and creating a Web page

  • Setting page properties and adding the XmlFormView control

  • Configuring SharePoint Server and resetting IIS

  • Debugging the custom Web page

Each procedure in this section builds on the previous procedure, so it is important that you do them in order.

NoteNote

Visual Studio 2005 must be installed on the server running InfoPath Forms Services.

Opening the SharePoint Site and Creating a Web Page

The following procedure creates the custom Web page at port 80 using the root site collection. Although port 80 is the default site collection of a SharePoint server, you can use any SharePoint site and are not limited to using the root site collection.

For information about how to deploy a custom Web page to a site other than the root site collection, see Deploying Custom Web Pages to a Non-Root SharePoint Site.

To open a SharePoint site and create a Web page

  1. Click Start, click Administrative Tools, and then click Internet Information Services (IIS) Manager.

  2. Click the plus sign (+) next to ServerName (local computer) (where ServerName is the name of your server), to expand the list of IIS services available on the server.

  3. Expand Web sites to show the list of Web sites that are managed by IIS.

  4. Right-click SharePoint - 80, and then click Properties.

  5. On the ASP.NET tab of the SharePoint - 80 Properties dialog box, highlight and copy the text in the File location box, without copying the web.config that may appear at the end of the File location string. This string typically looks like one of the following paths:

    • C:\Inetpub\wwwroot\wss\VirtualDirectories\80

    • C:\Inetpub\wwwroot\wss\VirtualDirectories\GUID

  6. Start Visual Studio 2005.

  7. On the File menu, click Open, and then click Web Site.

  8. Click File System, and in the Folder box, paste the file location path you copied (see Figure 1), and then click Open.

  9. Right-click the path of the Web site in Solution Explorer and select New Folder.

  10. Type a name for the folder, such as XmlFormView.

  11. Right-click the new folder and select Add New Item. The default template, Web Form, should be selected.

  12. Type a name for the new Web form a name, such as MyCustomPage, and select Visual C# as the Language.

  13. Select Place code in separate file, and then click Add.

    Figure 1. Opening SharePoint site using Visual Studio 2005 file path

    Opening SharePoint site

Setting Page Properties and Adding the XmlFormView Control

All custom Web pages that include the XmlFormView control require the page to have session state enabled and other default options either removed or modified. Follow these steps to set up the page to host the XmlFormView control, add the XmlFormView control to the Visual Studio Toolbox, and then add it to the Web page.

To set page properties and add the XmlFormView control

  1. In the Document Properties for the new Web form, click True for the EnableSessionState property.

  2. Click Source in the lower-left corner to view the page in source mode.

  3. Remove the default Doctype declaration tag. This tag begins with <!DOCTYPE html PUBLIC.

  4. Modify the Body tag to contain the following style attribute:

    style="margin: 0px;overflow:auto;padding:20px"
    
  5. Modify the Form tag to contain the following enctype attribute:

    enctype="multipart/form-data"
    

Note

The encoding type (enctype) attribute is necessary only if you intend to use the File Attachment control in a form loaded into the XmlFormView control. If this attribute is not set correctly, the File Attachment control will appear to function but will not upload a file to the server that is running InfoPath Forms Services.

  1. When you are finished, your page should look similar to the following:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyCustomPage.aspx.cs" Inherits="CustomPage_MyCustomPage" EnableSessionState="True" %>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body style="margin: 0px;overflow:auto;padding:20px">
        <form id="form1" runat="server" enctype="multipart/form-data">
        <div>
    
        </div>
        </form>
    </body>
    </html>
    
  2. Click Design in the lower-left corner to view the page in design mode.

  3. Expand the Toolbox, usually located on the left side in Visual Studio, and add the XmlFormView control to the General section:

    1. In the Toolbox, collapse the Standard section and expand the General section.

    2. Right-click underneath the General title and select Choose Items.

    3. In the Choose Toolbox Items dialog box, on the .NET Framework Components tab, select XmlFormView, and then click OK.

      If you do not see XmlFormView, click Browse and locate the Microsoft.Office.InfoPath.Server.dll assembly. It is typically located at Drive:\Program Files\Microsoft Office Servers\12.0\Bin, where Drive is the location where Office Forms Server 2007 or Office SharePoint Server 2007 is installed.

    4. Select the Microsoft.Office.InfoPath.Server.dll assembly, and then click OK.

    5. In the Choose Toolbox Items dialog box, select XmlFormView, and then click OK.

  4. In the General section of the Toolbox, drag the XmlFormView control to the new page. Then your new page should look similar to Figure 2.

  5. Select the XmlFormView1 control on the page.

  6. In the Data Binding section of the control's property sheet, paste the URL to browser-enabled form template in the XsnLocation property of the control.

    For information about how to create and publish a browser-enabled form template to use for this value, see Loading Browser-Enabled InfoPath Form Templates into the Control.

  7. Save the new Web form.

    Figure 2. XmlFormView control in a blank Web page

    XmlFormView control in a blank Web page

Configuring SharePoint Server and Resetting IIS

Before you can successfully debug your custom Web page in Visual Studio, you must use the Start Options command on the Website menu to specify the URL to the page on the SharePoint site. Then you save the Visual Studio solution, and reset the IIS service so that SharePoint Server will recognize your changes.

To configure SharePoint Server and reset IIS

  1. In Visual Studio, on the Website menu, click Start Options.

  2. In the Start action section, click Start URL, and then type the URL to your custom page, for example:

    http://ServerName/XmlFormView/MyCustomPage.aspx

    Where ServerName is the name of your server, XmlFormView is the name of the folder that you created in Visual Studio, and MyCustomPage is the name that you used to save the Web form page.

  3. Click OK.

  4. On the File menu, click Save All, and give the solution a name and location.

  5. Click Start, click All Programs, click Microsoft Visual Studio 2005, click Visual Studio Tools, and then open a Visual Studio 2005 Command Prompt window.

  6. At the command prompt, type iisreset and press ENTER.

  7. After Internet services successfully restarted appears, close the Visual Studio 2005 Command Prompt window.

Debugging the Custom Web Page

To debug the custom Web page, you must modify the Web options for the SharePoint site, contained in the web.config file. The following procedure automatically adds this flag to the web.config file.

To debug the Web page

  1. Save the custom Web page.

  2. Press F5 to start debugging.

    If you receive a message that states the web.config file not being configured for debugging, click OK. This automatically adds the debug flag to the web.config file.

If you do not have a browser-enabled form template to test the XmlFormView1 control, use the following procedures to create and publish a basic browser-enabled form template.

Loading Browser-Enabled InfoPath Form Templates into the Control

This section shows how to create a browser-enabled form template and publish it to a SharePoint site.

The XsnLocation property of an XmlFormView control must point to a form template published on the server. The form template must be browser-enabled, and it must reside in the same site collection as the custom Web form.

The easiest way to create a form template that can be displayed in an XmlFormView control is to create a new blank form template that is compatible with InfoPath and InfoPath Forms Services. A form template that is compatible with InfoPath and InfoPath Forms Services is known as a browser-compatibleform template. A form template that is published to a server that is running InfoPath Forms Services is known as a browser-enabled form template.

NoteNote

Some controls, structures, and data connections that are available in standard form templates are not available in Web-compatible form templates. Exceptions such as these to the Design Once feature set of Office InfoPath 2007 can be displayed in the Design Checker task pane when the form template's compatibility setting is InfoPath and InfoPath Forms Services. For more information about the features that work with InfoPath Forms Services, see Introduction to browser-compatible form templates.

To design the example form template

  1. Open Office InfoPath 2007 and, in the Getting Started dialog box, click Design a Form Template.

  2. In the Design a Form Template dialog box, click Form Template, click Blank, and then click Enable browser-compatible features only, as shown in Figure 3.

    Figure 3. Creating a form in the Design a Form Template dialog box

    Creating a form

  3. Click OK.

  4. In the task pane, click Controls.

  5. Add a Date Picker control to the form.

  6. Right-click the Date Picker and select Date Picker Properties.

  7. Click the function button to the right of the Value box.

  8. In the Insert Formula dialog box, click Insert Function.

  9. Click the today function, and then click OK.

  10. Click OK two more times to close all dialog boxes. This inserts the current date in the Date Picker control when the form is opened.

  11. Add an Optional Section to the form.

  12. Right-click the Optional Section and select Section Properties.

  13. On the Data tab, click Include the section in the form by default.

  14. On the Display tab, click Conditional Formatting.

  15. In the Conditional Format dialog box, click Add.

  16. Confirm that the first drop-down box contains field1, the field bound to the Date Picker control. Otherwise, change it to field1.

  17. In the third box under If this condition is true, click the drop-down list and select Use a formula.

  18. In the Insert Formula dialog box, click Insert Function.

  19. Click the today function, click OK, and then click OK again.

  20. In the Conditional Format dialog box, open the Shading drop-down list and select a color, and then click OK.

  21. Repeat steps 15 through 19, and select a different color to use when field1 is later than today's date, and for when field1 is earlier than today's date.

    Use the second drop-down list to specify is greater than and is less than operations. This provides some interactivity so that you know the form is working in the XmlFormView control.

  22. When you are finished, click OK in the Conditional Format dialog box, and then click OK to dismiss the Section Properties dialog box.

    The final form will look similar to Figure 4.

    Figure 4. Final example form

    Final example form in InfoPath 2007

To publish the example form template

  1. On the File menu, click Save, and save the form template to the local hard disk.

  2. On the File menu, click Publish.

  3. In the Publishing Wizard dialog box, select To a SharePoint server with or without InfoPath Forms Services, and then click Next.

  4. Type the URL of your server that is running InfoPath Forms Services.

    Because the form template and the custom Web page must be located in the same site collection, publish the form template to the root site collection by entering http://ServerName, where ServerName is the name of the server that is running InfoPath Forms Services.

  5. Select Enable this form to be filled out by using a browser, select Document library, and then click Next.

  6. Click Create a new document library, and then click Next.

  7. Type a name for the new document library, and then click Next.

  8. Click Next to skip adding columns to the document library.

  9. Click Publish.

  10. After the form template is published, click Open this form in the browser in the Publishing Wizard dialog box.

  11. In the Address bar in Microsoft Internet Explorer, copy the XsnLocation portion of the URL, starting after the equal sign (=) with "http" and ending with ".xsn", and then close the browser window.

  12. In the Publishing Wizard dialog box, click Close.

Coding with the XmlFormView Control

After you create and publish your form template, you can use various methods to control the behavior of the XmlFormView control, such as changing the editing state, controlling whether the header and footer toolbars are visible, and controlling where the completed form is saved. You can control these options programmatically, or you can control them declaratively by setting properties in the Visual Studio user interface (UI). This section includes an example of changing the EditingStatus property.

You may also want to pass values from the Web page to the XmlFormView control, or pass values from the XmlFormView control to the Web page. Examples are provided for doing these tasks programmatically.

Controlling the Form's Editing Status

If you do not want the form to be displayed when the Web page loads, set the EditingStatus property of the XmlFormView control to Init. You can do this either in the properties of the XmlFormControl1 control or in the tag of the control instance in the source page, which would look similar to the following example.

<cc1:XmlFormView ID="XmlFormView1" runat="server" XsnLocation="~sitecollection/FormServerTemplates/MyForm.xsn" EditingStatus="Init">

In this control tag, ~sitecollection is used as a SharePoint variable to facilitate deploying the custom Web page to multiple site collections. If you do not need to deploy the custom Web page to multiple site collections, you can just enter the fully qualified URL. For more information about deploying a custom Web page to a site collection, see Deploying Custom Web Pages to a Non-Root SharePoint Site later in this article.

You can programmatically change the EditingStatus property of the form to XmlFormView.EditingStatus.Editing to initialize and render the form template in the XmlFormView1 control after the user performs some action on the Web page, such as clicking a button in the page. The following example, which assumes you have added a using Microsoft.Office.InfoPath.Server.Controls; directive to the top of the code-behind page, shows how to do this.

protected void Button1_Click(object sender, EventArgs e)
{
    XmlFormView1.EditingStatus = XmlFormView.EditingStatus.Editing;
}

Important

When the EditingStatusproperty is set to XmlFormView.EditingState.Init, the XmlForm property of the XmlFormView control cannot access the XmlForm class and any of its associated functionality. For example, the MainDataSource() property is not available and you cannot work with the form's underlying XML data. Before you can access the XmlForm class using the XmlForm property, the EditingStatusproperty must be set to XmlFormView.EditingState.Editing.

Passing a Value from the Form to the Web Page

For code in the page to be able to set the value of a field in the InfoPath form, it must be within an appropriate event handler. The XmlForm property of the XmlFormView control, which provides access to the XmlForm class and its MainDataSource() property for working with the underlying XML data of the form, can only be accessed during one of the following events:

Note

The Initialize event is executed whenever an InfoPath form is loaded into the XmlFormView control. This occurs when the Web page is first loaded, when the XsnLocation or XmlLocation properties of the XmlFormView control are set to point to a different form template or form, and when the EditingStatusproperty is modified from XmlFormView.EditingState.Init to XmlFormView.EditingState.Editing.

If the code is in an event handler of another control on the page, such as the System.Web.UI.WebControls.Button.Click handler of a button, the code must first call the inherited System.Web.UI.WebControls.WebParts.Part.DataBind method of the XmlFormView control before accessing the data of the form, as shown in the following example. This example assumes you have added using System.Xml; and using System.Xml.XPath; directives to the top of the code-behind page, and that you have an ASP.NET TextBox control on the page named TextBox1 to receive the data.

protected void Button2_Click(object sender, EventArgs e)
{
   // XmlFormView control's DataBind method to bind to 
   // its data source.
   XmlFormView1.DataBind();
   // Create an XPathNavigator positioned at the root of 
   // the form's main data source.
   XPathNavigator xNavMain = 
      XmlFormView1.XmlForm.MainDataSource.CreateNavigator();
   // Create an XmlNamespaceManager and add the "my" namespace
   // alias from the form's main data source.
   XmlNamespaceManager xNameSpace = 
      new XmlNamespaceManager(new NameTable());
   xNameSpace.AddNamespace("my", XmlFormView1.XmlForm.NamespaceManager.
      LookupNamespace("my").ToString());
   // Set TextBox1 on the page to the value in the form's field2.
   TextBox1.Text = xNavMain.SelectSingleNode(
      "/my:myFields/my:field2", xNameSpace).ToString();
}

Passing a Value from the Web Page to the Form

The following procedure sets the value of field2 in the form to the value in the TextBox1 control on the Web page whenever the Initialize event handler of the XmlFormView control executes. This example requires that you add a Text Box control to both the Web page and the InfoPath browser-enabled form template rendered in the XmlFormView control.

  1. In the Design view of your Web page, add a TextBox control to the page. In InfoPath design mode, add a Text Box control to your form template and republish it to the document library on the SharePoint site.

  2. Switch to the Source view of your Web page and locate the tag for the XmlFormView1 control. It should look similar to the following example.

    <cc1:XmlFormView ID="XmlFormView1" runat="server" Height="250px" Width="100%"
    XsnLocation="http://ServerName/DocumentLibrary/Forms/template.xsn">
    

    ServerName and DocumentLibrary correspond to the name of your server that is running InfoPath Forms Services and the name of the document library where your form template is published.

  3. Declare the name of the Initialize event handler in the XmlFormView1 control tag:

    <cc1:XmlFormView ID="XmlFormView1" runat="server" Height="250px" Width="100%"
    XsnLocation="http://ServerName/DocumentLibrary/Forms/template.xsn"
    OnInitialize="XmlFormView1_Initialize" />
    
    NoteNote

    Alternatively, you can double-click the blank text box next to this event in the Properties Windows when the XmlFormView1 control is selected.

  4. Right-click inside the Web page Source window, and then click View Code. This opens a new code-behind window.

  5. Add the following using statements.

    using System.Xml;
    using System.Xml.XPath;
    
  6. In the same window, add the code for the Initialize event handler.

    protected void XmlFormView1_Initialize(object sender, 
       InitializeEventArgs e)
    {
        // Create an XPathNavigator positioned at the root of 
        // the form's main data source.
        XPathNavigator xNavMain = XmlFormView1.XmlForm.MainDataSource.CreateNavigator();
        // Create an XmlNamespaceManager and add the "my" namespace
        // alias from the form's main data source.
        XmlNamespaceManager xNameSpace = new XmlNamespaceManager(new NameTable());
        xNameSpace.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-04-20T16:26:21");
        // Create an XPathNavigator positioned on the form's field2.
        XPathNavigator fTextBox1 = xNavMain.SelectSingleNode(
            "my:myFields/my:field2", xNameSpace);
        // Set the form's field2 to the value in TextBox1.
        fTextBox1.SetValue(TextBox1.Text);
    }
    
  7. In the call to the System.Xml.XmlNamespaceManager.AddNamespace method, substitute the correct XML namespace with that of the form template you are loading.

    Tip

    To get the value of the namespace, open the form template in Design mode, display the Data Source task pane, right-click myFields, click Properties, and then click Details.

  8. In the call to the System.Xml.XPath.XPathNavigator.SelectSingleNode method, change the XPath expression to select the field in the example, "my:myFields/my:field2", to the expression for the field in your form that you want to populate with the value from the text box in the Web page.

    TipTip

    To copy the XPath expression to select any field or group in the form's data source to the clipboard, right-click the field or group in the Data Source task pane, and then click Copy XPath.

Submitting Data from the Form

Office InfoPath 2007 provides a new kind of data connection for submitting data from a form based on a form template that is hosted in another environment back to the hosting environment, which in this case is to the custom Web page that contains the XmlFormView control. This new data connection, called the SubmitToHostAdapter, has the corresponding manifest.xsf entry in the form definition file (.xsf).

<xsf:submitToHostAdapter name="Submit" submitAllowed="yes"></xsf:submitToHostAdapter>

The following procedure sets the value of field2 in the form to the value in the TextBox1 control on the Web page whenever the SubmitToHost event handler of the XmlFormView control executes. This occurs when the user clicks the Submit button on the toolbar.

Follow these steps to create a SubmitToHostAdapter data connection in a form template that is hosted in a custom Web page.

To set up a new SubmitToHostAdapter data connection

  1. Open the InfoPath form template in design mode.

  2. On the Tools menu, click Data Connections.

  3. Click Add.

  4. In the Data Connection Wizard, click Create a new connection to and Submit data, and then click Next.

  5. Select To the hosting environment, such as an ASP.NET page or a hosting application, as shown in Figure 5, and then click Next.

    Figure 5. Submitting data to the host

    Submitting data to the host

  6. Type a name for the new data connection, and then click Finish.

After you create the new SubmitToHostAdapter data connection, enable the form to submit its XML data to the hosting environment by following these steps:

To enable the form to submit data

  1. On the Tools menu, click Submit Options.

  2. Select Allow users to submit this form.

  3. In the first drop-down list, select Hosting environment, and in the second list, select the name you gave to the data connection.

    It should look similar to Figure 6.

    Figure 6. Selecting a data connection

    Selecting a data connection

  4. Click OK.

  5. Republish the form template to the same document library on the SharePoint server, or create a new document library.

After you republish the form template, you must create an event handler for the SubmitToHost event of the XmlFormView control in your custom Web page. To do this, follow these steps:

To create the event handler

  1. Switch to the Source view of your Web page and locate the tag for XmlFormView1. It should look similar to the following example.

    <cc1:XmlFormView ID="XmlFormView1" runat="server" Height="250px" Width="100%" XsnLocation="http://ServerName/DocumentLibrary/Forms/template.xsn">
    

    ServerName and DocumentLibrary correspond to the name of the server that is running InfoPath Forms Services and your document library name.

  2. Declare the name of the SubmitToHost event handler in the XmlFormView1 control tag.

    <cc1:XmlFormView ID="XmlFormView1" runat="server" Height="250px" Width="100%"
    XsnLocation="http://ServerName/DocumentLibrary/Forms/template.xsn"  
    OnInitialize="XmlFormView1_Initialize"
    OnSubmitToHost="XmlFormView1_SubmitToHost" />
    
  3. Right-click inside the Web page Source window, and then click View Code. This opens the code-behind window. In this window, add the following SubmitToHost code.

    protected void XmlFormView1_SubmitToHost(object sender, 
       SubmitToHostEventArgs e)
    {
       // Create an XPathNavigator positioned at the root of 
       // the form's main data source.
       XPathNavigator xNavMain = 
          XmlFormView1.XmlForm.MainDataSource.CreateNavigator();
       // Create an XmlNamespaceManager and add the "my" namespace
       // alias from the form's main data source.
       XmlNamespaceManager xNameSpace = 
          new XmlNamespaceManager(new NameTable());
       xNameSpace.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-04-20T16:26:21");
       // Create an XPathNavigator positioned on the form's field2.
       XPathNavigator fTextBox1 = xNavMain.SelectSingleNode(
          "my:myFields/my:field2", xNameSpace);
       // Set TextBox1 on the page to the value in the form's field2.
       TextBox1.Text = xNavMain.SelectSingleNode(
          "/my:myFields/my:field2", xNameSpace).ToString();
    }
    

Substitute the correct XML namespace with that of the form template you are loading into the XmlFormView control, and change the XPath of the field in the example, "my:myFields/my:field2", to the XPath of the field in your form that will be used to populate the Text Box control on the Web page.

When the form is submitted, TextBox1 will be set to the value in my:field2.

An alternative method of passing a value from the form to the Web page is to use the NotifyHost event handler and the value passed to the Notification property by the NotifyHost(String) method of the XmlForm class in the form template's business logic code, rather than the SubmitToHost event handler, which is triggered by submitting the form. Note that using the NotifyHost event handler means that the XML data is not validated before making the form data available to the Web page.

Code Example

The following example contains the complete code for the code-behind page.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Office.InfoPath.Server.Controls;
using System.Xml;
using System.Xml.XPath;

public partial class XmlFormView_MyPage : System.Web.UI.Page
{
   protected void Button1_Click(object sender, EventArgs e)
   {
      XmlFormView1.EditingStatus = XmlFormView.EditingState.Editing;
   }

   protected void Button2_Click(object sender, EventArgs e)
   {
      XmlFormView1.DataBind();
      XPathNavigator xNavMain =
         XmlFormView1.XmlForm.MainDataSource.CreateNavigator();
      XmlNamespaceManager xNameSpace = 
         new XmlNamespaceManager(new NameTable());
      xNameSpace.AddNamespace("my",XmlFormView1.XmlForm.
        NamespaceManager.LookupNamespace("my").ToString());
      TextBox1.Text = xNavMain.SelectSingleNode(
         "/my:myFields/my:field2",xNameSpace).ToString() ; 
   }

   protected void XmlFormView1_Initialize(object sender,
      InitializeEventArgs e)
   {
      XPathNavigator xNavMain =
         XmlFormView1.XmlForm.MainDataSource.CreateNavigator();
      XmlNamespaceManager xNameSpace = new XmlNamespaceManager(
         new NameTable());
      xNameSpace.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-03-27T02:05:51");
      XPathNavigator fTextBox = xNavMain.SelectSingleNode(
         "/my:myFields/my:field2", xNameSpace2);
      fTextBox.SetValue(TextBox1.Text);
   }

   protected void XmlFormView1_SubmitToHost(object sender, 
      SubmitToHostEventArgs e)
   {
      XPathNavigator xNavMain = 
         XmlFormView1.XmlForm.MainDataSource.CreateNavigator();
      XmlNamespaceManager xNameSpace = 
         new XmlNamespaceManager(new NameTable());
      xNameSpace.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-04-20T16:26:21");
      XPathNavigator fTextBox1 = xNavMain.SelectSingleNode(
         "my:myFields/my:field2", xNameSpace);
      TextBox1.Text = xNavMain.SelectSingleNode(
         "/my:myFields/my:field2", xNameSpace).ToString();
   }
}

Deploying Custom Web Pages to a Non-Root SharePoint Site

When you create a custom Web page as described earlier in this article, the .aspx page can belong only to the root site collection of the SharePoint server. To create a page under a non-root site collection, you must deploy the custom Web page as a SharePoint feature. A SharePoint feature is a package of Windows SharePoint Services elements that can be activated for a specific scope to perform a particular goal or task.

A SharePoint feature requires two simple XML files and is deployed using the stsadm.exe command-line tool on the server, which you can automate by using a batch file.

To deploy a Web page as a SharePoint feature

  1. Create a folder under the local Features folder on your server, typically located at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\FEATURES.

  2. Add your custom .aspx page to this folder.

  3. Create the Feature.xml and Module.xml files based on the following examples, and add them to the same location.

    Sample Feature.xml

    <?xml version="1.0"?>
    <Feature Id="8C4DD0CB-5A94-44da-9B7F-E9ED49C2B2DC" Title="Custom Web page"
    Description="This simple example feature adds an aspx page with a hosted
    XmlFormView control" Version="1.0.0.0" Scope="Web"
    xmlns="http://schemas.microsoft.com/sharepoint/">
    <ElementManifests>
        <ElementManifest Location="Module.xml"/>
    </ElementManifests>
    </Feature>
    

    In the Feature.xml file, set the value of the Id attribute of the Feature element to a GUID generated by using the Create GUID command on the Tools menu in Visual Studio. Alternatively, you can create a simple console application to create a GUID using the following line of C# code:

    System.Console.WriteLine(System.Guid.NewGuid().ToString().ToUpper());
    

    You only have to set the Id attribute value one time for a given feature. If you are upgrading an existing feature, you do not have to generate a new GUID for the Id attribute each time you upgrade the feature.

    Sample Module.xml

    <?xml version="1.0"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
        <Module Name="file" Url="" Path="">
            <File Url="XmlFormViewPage.aspx" />
        </Module>
    </Elements>
    

    In the Module.xml file, set the value of the Url attribute of the File element to the name of your .aspx page.

  4. Deploy the feature to SharePoint Server. The command-line syntax to deploy a feature is as follows.

    C:\Program Files\Common Files\Microsoft Shared\Web Server
    Extensions\12\bin\stsadm -o installfeature –filename
    FeatureFolderName\Feature.xml
    
  5. Activate the feature on a SharePoint site collection. The command-line syntax to activate a feature is as follows.

    C:\Program Files\Common Files\Microsoft Shared\Web Server
    Extensions\12\bin\stsadm -o activatefeature –filename
    FeatureFolderName\Feature.xml -url http://ServerName/Sitecollection
    

    Replace ServerName and SiteCollection with the actual names of your SharePoint server and the site collection name to which the feature will be activated. You can create a batch file and run these command lines sequentially.

    NoteNote

    Instead of using a fully-qualified server name, you can use the ~sitecollection.

In a real-world scenario, a custom Web page that contains an XmlFormView control is not located on the root site of a SharePoint server. However, the recommended method of developing a custom Web page is from the root site of the SharePoint server as shown in this article. When the Web page is ready to be deployed, a custom feature package can be used to deploy the Web page to a site collection as described in the section Deploying Custom Web Pages to a Non-Root SharePoint Site.

When you are deploying to a site collection, replace all fully-qualified URLs with tokenized URLs that will function in the site collection. For example, an XsnLocation parameter references a form template in the following location.

XsnLocation="http://ServerName/SiteCollectionName/DocumentLibraryName/Forms/template.xsn"

When the Web page is deployed to a site collection, the ~sitecollection token is substituted for the ServerName and SiteCollectionName values.

XsnLocation="~sitecollection/DocumentLibraryName/Forms/template.xsn"

Considerations When Using the XmlFormView Control

Consider the following issues when you are using the XmlFormView control in a custom Web page.

  • Only one XmlFormView control can be added per Web form (.aspx page).

  • For security reasons, the XsnLocation, XmlLocation, and SaveLocation properties of the XmlFormView control must specify locations in the same site collection as the custom page. For the example custom page created using the procedure described earlier, the value specified for the XsnLocation property is in the root site collection on the SharePoint server.

  • InfoPath Forms Services, as part of either Microsoft Office Forms Server 2007 or Microsoft Office SharePoint Server 2007, is required in order to render the form inside the XmlFormView control.

  • The custom Web page containing the XmlFormView control should be located in the same IIS Web application as SharePoint Server. For more information about using a different Web application, see the InfoPath Team Blog entry Forms Services and multiple IIS Web Applications.

Conclusion

You can use the XmlFormView control to provide interactive InfoPath forms to users in a custom Web page that does not require the InfoPath 2007 client to be installed on the user's computer. The XmlFormView control is present on servers running InfoPath Forms Services, as part of either Microsoft Office Forms Server 2007 or Microsoft Office SharePoint Server 2007. The various properties, methods, and events of the control enable you to seamlessly integrate InfoPath forms into an existing Web infrastructure.

Additional Resources

For more information about developing with InfoPath, see the following resources: