How To: Display browser enabled InfoPath forms in a web part supporting connections

Here is a little something from one of the chapters in the book (

With MOSS, there is the new Forms Services capability that allows you to take an InfoPath designed form and present it to the user as a web based form. When associated with a document library, the user can click New and the page opens displays the web-based form in a full page view of the browser. The user fills out the form and clicks save resulting in the xml being stored as an item in the library. Another feature that is well documented is that the Forms Server capability of MOSS also ships a ASP.NET server control ( that you can use in your own custom ASP.NET pages. This control is located in 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.

Upon closer examination of this ASP.NET server control, you may notice that it is in fact a web part. But this is not a web part that you would want to add to the SafeControls list and drag into a site as it expects to run in a full screen mode and will obliterate everything else on the page. I have had a vision that when viewing the items in the form library, a user should be able to open the form on the same page as the list of form instances. The solution is a simple one. I created a custom web part that wraps the out of the box control and adds support for connections. This way it can be dragged onto the AllItems.aspx page and connected to the view of the form library items. So a user can select a form, open it, edit the data, and save it back all in the same screen.

Here is the code form the web part.

Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.WebControls
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.WebControls
Imports Microsoft.Office.InfoPath.Server.Controls
Imports System.Xml

Public Class FormViewWebPart
    Inherits System.Web.UI.WebControls.WebParts.WebPart

    Const defaultXmlLocation = ""

    Private m_xmlLocation As String = defaultXmlLocation
    Private WithEvents m_xmlFormView As XmlFormView
    Private m_errorMessage As String = String.Empty

    <WebBrowsable(), Personalizable(PersonalizationScope.User), WebDisplayName("XMLLocation"), WebDescription("URL of web-enabled InfoPath form")> _
    Public Property XMLLocation() As String
            Return m_xmlLocation
        End Get
        Set(ByVal value As String)
            m_xmlLocation = value
        End Set
    End Property

    Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)

        If m_errorMessage <> String.Empty Then
            If (Me.m_xmlLocation.Length > 0) Then
                m_xmlFormView.XmlLocation = m_xmlLocation
                m_xmlFormView.Visible = True
            End If

        End If
    End Sub

    Protected Overrides Sub CreateChildControls()
        m_xmlFormView = New XmlFormView()
        m_xmlFormView.Visible = False

        m_xmlFormView.EditingStatus = XmlFormView.EditingState.Editing

    End Sub

    Private Sub m_xmlFormView_Initialize(ByVal sender As Object, ByVal e As Microsoft.Office.InfoPath.Server.Controls.InitializeEventArgs) Handles m_xmlFormView.Initialize
    End Sub

   <ConnectionConsumer("XMLLocation")> _
    Public Sub GetConnectionInterface(ByVal providerPart As IWebPartField)
        Dim callback As FieldCallback = New FieldCallback(AddressOf Me.ReceiveField)
    End Sub

    Public Sub ReceiveField(ByVal field As Object)
        If (field IsNot Nothing) Then
            Me.m_xmlLocation = CType(field, String)
        End If

    End Sub

End Class