Share via


Passing query string to a Infopath 2007 form

Support
of infopath 2007 browser rendering has opened a whole lot of options
for web forms. Now instead of creating a form in ASPX , one can create
an infopath form and take advantage of various submitting options like
submitting to web service, database etc.

One feature which I
find hard to get is passing parameters to infopath form so that it can
dynamically present the data to user. For Ex you can pass a customer id
to the infopath form which updates the database based on that customer
ID.

Normally rendering the infopath form using document library
prevents us to pass our own parameters throught the URL since the URL
is automatically generated by the Forms Server.

The trick is to use XMLFormView control as described in this link
https://msdn2.microsoft.com/en-us/ms778201.aspx

Observe
the code in OnInitialize event, here you can grap the parameter in the
query string and send it to your infopath form to a text box. That text
box can be set to hidden if possible.

 protected void _xmlFormView1_OnInitialize(object sender, EventArgs e)
{
 XPathNavigator xNavMain = XmlFormView1.XmlForm.MainDataSource.CreateNavigator();
 XmlNamespaceManager xNameSpace = new XmlNamespaceManager(new NameTable());
 xNameSpace.AddNamespace("my", "https://schemas.microsoft.com/office/infopath/2003/myXSD/2006-04-20T16:26:21");
 XPathNavigator fTextBox1 = xNavMain.SelectSingleNode("my:myFields/my:field2", xNameSpace);
 fTextBox1.SetValue(Request.QueryString["CustomerID"]);
}

This way you can pass the dynamic parameters to the infopath form rendered in the browser.
Note that the namespace specified here must be exactly same as that specified in your form.
To find out your namespace, rename the .xsn file to .cab and open the template.xml file.

Happy Working :)