It would be very common for someone working on InfoPath code behind to retrieve values and set values on the form using code. Below are the snippet for both in C#.
/// <summary>
/// Sets the field value of an InfoPath Form. pass isNil true for date fields
/// </summary>
/// <param name="xPath">copy the xpath from InfoPath designer for the field</param>
/// <param name="xValue"> the value to be set</param>
/// <param name="isNil">true for date fields</param>
private void SetFieldValue(string xPath,string xValue, bool isNil)
{
try
{
XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();
XPathNavigator xnMyField = xnDoc.SelectSingleNode(xPath, this.NamespaceManager);
if (isNil)
{
if (xnMyField.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))
xnMyField.DeleteSelf();
}
xnMyField.SetValue(xValue);
xnDoc = null;
xnMyField = null;
}
catch (Exception ex)
{
throw new Exception(xPath + ":" + xValue + ":" + ex.StackTrace, ex);
}
Code for getting field values from InfoPath in code behind using VSTA in C#
/// <summary>
/// Gets the field value from InfoPath Form
/// </summary>
/// <param name="xPath">copy the xpath from the InfoPath Designer for the field values to be retrieved</param>
/// <returns></returns>
private string GetFieldValue(string xPath)
{
XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();
XPathNavigator xnMyField = xnDoc.SelectSingleNode(xPath, this.NamespaceManager);
if (xnMyField != null)
{
return xnMyField.Value;
}
else
{
return string.Empty;
}
}
Advertisements
Thanks for posting this, it was very helpful to me as a InfoPath newbie.
Please correct the page width or CSS issues; lines are not wrapping properly and code lines hidden.
Hi
I am newbie in InfoPath 2010. I have three text fields in my form and I want to calculate average of them and show it in another text box. My fields are not grouped.
Many thanks,
Monika
///
/// Sets the field value of an InfoPath Form. pass isNil true for date fields
///
/// copy the xpath from InfoPath designer for the field
/// the value to be set
/// true for date fields
private void SetFieldValue(string xPath,string xValue, bool isNil)
{
try
{
XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();
XPathNavigator xnMyField = xnDoc.SelectSingleNode(xPath, this.NamespaceManager);
if (isNil)
{
if (xnMyField.MoveToAttribute(“nil”, “http://www.w3.org/2001/XMLSchema-instance”))
xnMyField.DeleteSelf();
}
xnMyField.SetValue(xValue);
xnDoc = null;
xnMyField = null;
}
catch (Exception ex)
{
throw new Exception(xPath + “:” + xValue + “:” + ex.StackTrace, ex);
}
Code for getting field values from InfoPath in code behind using VSTA in C#
///
/// Gets the field value from InfoPath Form
///
/// copy the xpath from the InfoPath Designer for the field values to be retrieved
///
private string GetFieldValue(string xPath)
{
XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();
XPathNavigator xnMyField = xnDoc.SelectSingleNode(xPath, this.NamespaceManager);
if (xnMyField != null)
{
return xnMyField.Value;
}
else
{
return string.Empty;
}
}