Sunday, September 03, 2006

Rendering an InfoPath 2007 form in a web part

The white paper - Hosting the InfoPath 2007 Form Editing Environment in a Custom Web Form shows how you can use the XMLFormView control to load InfoPath forms in a web page, but it is also quite easy to do this in a SharePoint web part. This posting will show you how to do this. For some nice screenshots, check out Patricks posting, unfortunately he forgot to post some code ;-) ...

If you want to use the XMLFormviewer control, you will first need tot add a reference to the Microsoft.Office.InfoPath.Server dll - which you can find it in the Program Files\Microsoft Office Servers\12.0\bin directory. The XMLFormviewer control determines which InfoPath form to load through the XSNLocation property - so I will create a web part property for this as well.

It is also quite easy to read/write fields within the InfoPath fields - this provides some interesting possibities in combination with the fact that you can easily define web part connections.  You can do this from within the OnInitialize event handler without having to call the DataBind. To access the InfoPath fields you can use the new InfoPath 2007 object model - check out Design once and the new InfoPath 2007 Object Model . Add a reference to the Microsoft.Office.InfoPath.dll ( Program Files\Microsoft Office Servers\12.0\bin directory ) to manipulate the InfoPath form. Tip: Retrieve the  xNavMain.InnerXml first - and use a program such as Visual XPath to find the appropriate namespace and XPath expression.

 

namespace WebParts
{
public class InfoPathViewerWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
private const string defaultxsnlocation = "";
private string _xsnlocation = defaultxsnlocation;
private XmlFormView viewform;

[Personalizable(), WebBrowsable(),
WebDisplayName("XSNLocation"),
WebDescription("URL of the web-enabled InfoPath form to display")]
public string XSNLocation
{
get { return _xsnlocation; }
set { _xsnlocation = value; }
}


protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
base.RenderContents(writer);
this.EnsureChildControls();
}

protected override void CreateChildControls()
{
base.CreateChildControls();
viewform = new XmlFormView();
if (_xsnlocation.Length > 0)
{
viewform.XsnLocation = _xsnlocation;
}
viewform.Initialize += new EventHandler<InitializeEventArgs>(viewform_Initialize);

this.Controls.Add(viewform);
}

void viewform_Initialize(object sender, InitializeEventArgs e)
{
try
{
XPathNavigator xNavMain = viewform.XmlForm.MainDataSource.CreateNavigator();

XmlNamespaceManager xNameSpace = new XmlNamespaceManager(new NameTable());
xNameSpace.AddNamespace("my", "http://.../office/infopath/2003/myXSD/2006-08-07T07:37:16");
XPathNavigator fSummary = xNavMain.SelectSingleNode("/my:myFields/my:Summary", xNameSpace);
if (fSummary != null)
{
fSummary.SetValue("Hello InfoPath");
}
else
{
EventLog.WriteEntry("InfoPathWebPart", "fSummary not found", EventLogEntryType.Information);
}

}
catch (Exception ex)
{
EventLog.WriteEntry("WebParts.InfoPathViewerWebPart", ex.ToString(),EventLogEntryType.Error);
}

}


}

 


 


Tags: , , , , , , ,


Currently listening: Alien Ant Farm - ANThology

5 comments:

  1. Anonymous2:23 PM

    Hey Hey!!! I did not forget the code. The XmlFormView control is actually a Web Part :). Look at it with the Reflector

    Patrick

    ReplyDelete
  2. Anonymous6:18 AM

    See similar post in
    http://madhurahuja.blogspot.com/2006/08/hosting-infopath-form-on-sharepoint.html

    ReplyDelete
  3. Anonymous11:36 AM

    Hey, very helpful example! Thanks!

    ReplyDelete
  4. Anonymous9:51 PM

    Hi,

    wow, that`s what I`m looking for

    how can I use this?
    I got an unexpected error on Page load.
    I`m debug the process but I can`t see the error.
    in debug-mode after 4-5 times on calling the line "_xsnlocation = defaultxsnlocation;" I became the error.

    can anybody help me please?

    thx
    Daniel

    ReplyDelete
  5. Very useful piece of information and straight to the point. Thanks!

    ReplyDelete