InfoPath get/set form fields value in code behind


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

4 Comments on “InfoPath get/set form fields value in code behind”

  1. Alan W March 23, 2011 at 12:46 pm #

    Thanks for posting this, it was very helpful to me as a InfoPath newbie.

  2. Boby Thompson March 29, 2011 at 11:09 pm #

    Please correct the page width or CSS issues; lines are not wrapping properly and code lines hidden.

  3. Monika April 12, 2012 at 7:36 am #

    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

  4. fermknsr July 1, 2013 at 9:05 am #

    ///
    /// 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;
    }

    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: