InfoPath 2007 - Programmatically Create a Repeating Section or Repeating Table Based on Number of Characters in a Text Field - InfoPath Dev
in

InfoPath Dev

InfoPath 2007 - Programmatically Create a Repeating Section or Repeating Table Based on Number of Characters in a Text Field

Last post 04-06-2009 08:30 AM by jhvrealm. 5 replies.
Page 1 of 1 (6 items)
Sort Posts: Previous Next
  • 04-01-2009 10:28 AM

    InfoPath 2007 - Programmatically Create a Repeating Section or Repeating Table Based on Number of Characters in a Text Field

    I am relatively new to Infopath. I tried this post for assistance in this matter several months ago. I was taken off to perform other duties and am now returning to this issue.

    Background:
    - I am using InfoPath 2007.
    - I inherited this form from another developer using InfoPath 2003.
    - I am not sure if the form is set up for managed code - how do I check?
    - I am not sure if the form is set up as rich client only or browser-compatible (form is submitted to and launched from a SharePoint server) - how do I check?

     My challenge:
    – Transfer text data entered by a user from the main view of an InfoPath form into a second view (export view) specifically configured for export into Excel. Data exported to Excel must be structured in such a way that the Excel can import directly into a database.

    Requirements:
    – A user enters information into a text field within a repeating section in the main view that limits text input to 585 characters max.
    – This text data must be transferred to the export view into a repeating component (section or table) that allows for separate text fields each having a limit of 65 characters.

    I can easiily separate the text from the user's input (using substring) into nine separate text fields (with each text field having a 65 character max to allow for the user's 585 max) within a repeating section in the export view. The problem is with the export into Excel. The data exported into Excel must be structured in a manner that allows for direct import into our database. When separate text fields are used as described above, the data exports 65 characters per cell, all into the same row. I need each of the 65 characters to be input into a cell in successive rows.

    If I limit the user's input to 65 characters per text field, require they repeat the section to add more information, and set it up so that the section repeated by the user is automatically repeated in the corresponding section in the export view, the export into Excel is correct. The problem is, this method is too difficult for the user to enter information.

    As far as I can determine, each time a section is repeated through clicking on the associated control, the resulting export into Excel is such that each repeating component is assigned to a successive row in Excel. This is exactly what I need to occur.

    What I would like to do then is allow for the 585 character max for the user's input of information in the main view, test the length of the input, use substring to separate into 65 character portions, and programmatically repeat a repeating section in the export view to accept each individual character portion.

    Basically, how do I programmatically perform the same function as when a user repeats a section by clicking the control that results in creating another repeating section?

    Any help I can get will be greatly appreciated!!!!

     Best Regards,

    jhvrealm

  • 04-01-2009 01:27 PM In reply to

    Re: InfoPath 2007 - Programmatically Create a Repeating Section or Repeating Table Based on Number of Characters in a Text Field

    Regarding Background info: I determined the form was developed in Jscript.
    Question: I removed the Jscript code in a copy of the form (Tools/Form Options/Programming; Remove Code) and designated C# as the form template code language. Does this break the form in any way? After a quick review, there seems to be no negative effect, including exporting into Excel.

  • 04-01-2009 06:15 PM In reply to

    Re: InfoPath 2007 - Programmatically Create a Repeating Section or Repeating Table Based on Number of Characters in a Text Field

    It's entirely possible (and likely) that removing the code from a form template would remove some of the template's functionality and cause it to not work as expected.  I recommend you determine just what the JScript is accomplishing before removing it.

    As for adding rows to repeating sections, please check out this quick and simple blog post and try the method it describes:

    http://www.infopathdev.com/blogs/jimmy/archive/2009/03/06/automatically-adding-rows-to-repeating-sections.aspx

    If you need any assistance setting the data in the repeating section, please let us know.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 04-02-2009 01:07 PM In reply to

    Re: InfoPath 2007 - Programmatically Create a Repeating Section or Repeating Table Based on Number of Characters in a Text Field

    Thank you for the reply, Jimmy.

    Concerning removing code, the only custom code was on buttons to move text fields dynamically upward or downward along a stack of text fields. I know this is unclear, however the problem can be fixed if a C# version of the following code could be provided:

    //=======
    function btnMoveUp::OnClick(eventObj)
    {
              var oRow = eventObj.Source;
              var oRows = oRow.parentNode;
              var oPreviousRow = oRow.selectSingleNode("preceding-sibling::my:Export_CB_Step_Details_Section[1]");
              oRows.insertBefore(oRow, oPreviousRow);
    }
    //=======
    function btnMoveDown::OnClick(eventObj)
    {
              var oRow = eventObj.Source;
              var oRows = oRow.parentNode;
              var oNextRow = oRow.selectSingleNode("following-sibling::my:Export_CB_Step_Details_Section");
              oRows.insertBefore(oNextRow, oRow);
    }
    //=======

     Concerning the primary issue, I want to add a Repeating Section, not a row in a Repeating Section. Can you help?

     

  • 04-02-2009 08:43 PM In reply to

    Re: InfoPath 2007 - Programmatically Create a Repeating Section or Repeating Table Based on Number of Characters in a Text Field

    This is the equivalent C# code:

    public void CTRL5_5_Clicked(object sender, ClickedEventArgs e)
    {
        // Move row down

        XPathNavigator oRow = e.Source;
        XPathNavigator oRows = oRow.SelectSingleNode("..");
        XPathNavigator oNextRow = oRow.SelectSingleNode("following-sibling::my:group2", NamespaceManager);
        if (oNextRow != null)
        {
            oNextRow.InsertAfter(oRow);
            oRow.DeleteSelf();
        }
    }

    public void CTRL6_5_Clicked(object sender, ClickedEventArgs e)
    {
        // Move row up
        XPathNavigator oRow = e.Source;
        XPathNavigator oRows = oRow.SelectSingleNode("..");
        XPathNavigator oPrevRow = oRow.SelectSingleNode("preceding-sibling::my:group2[1]", NamespaceManager)
        if (oPrevRow != null)
        {
            oPrevRow.InsertBefore(oRow);
            oRow.DeleteSelf();
        }
    }

    As for your original question, supposing I had a data source like this:

    myFields
        dataGroups
           dataGroup (repeating)
            longText
            dataParts
                dataPart (repeating text field)
    I could break it up with code like this:

    string myNamespace = NamespaceManager.LookupNamespace("my");
    foreach (XPathNavigator group in MainDataSource.CreateNavigator().Select("/my:myFields/my:dataGroups/my:dataGroup", NamespaceManager))
    {
        string textToProcess = group.SelectSingleNode("my:longText", NamespaceManager).Value;
        XPathNavigator partsGroup = group.SelectSingleNode("my:dataParts", NamespaceManager);
        // Remove any existing nodes in the dataParts group
        while(partsGroup.MoveToChild("part", myNamespace))
        {
        partsGroup.DeleteSelf();
        }
        using (XmlWriter writer = partsGroup.AppendChild())
        {
            int currentPos = 0;
            string textToAdd;
            while (currentPos < textToProcess.Length)
            {
                if (currentPos + 65 > textToProcess.Length)
                {
                    textToAdd = textToProcess.Substring(currentPos);
                }
                else
                {
                    textToAdd = textToProcess.Substring(currentPos, 65);
                }
                writer.WriteStartElement(
    "my", "part", myNamespace);
                writer.WriteValue(textToAdd);
                writer.WriteEndElement();
                currentPos += 65;
            }
        }
    }

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 04-06-2009 08:30 AM In reply to

    Re: InfoPath 2007 - Programmatically Create a Repeating Section or Repeating Table Based on Number of Characters in a Text Field

    Thanks again, Jimmy.

    As far as moving the text fields up and/or down, the code that worked and prevented duplications of text fields was very close to yours, as follows:

            public void btnMoveUp_Clicked(object sender, ClickedEventArgs e)
            {
                // Write your code here.
                // Move row up
                XPathNavigator oRow = e.Source;
                XPathNavigator oRows = oRow.SelectSingleNode("..");
                XPathNavigator oPrevRow = oRow.SelectSingleNode("preceding-sibling::my:Export_CB_Step_Details_Section[1]", NamespaceManager);
                if (oPrevRow != null)
                {
                    oRow.InsertBefore(oPrevRow);
                    oPrevRow.InsertAfter(oRow);
                    oRow.DeleteSelf();
                    oPrevRow.DeleteSelf();
                }
            }

            public void btnMoveDown_Clicked(object sender, ClickedEventArgs e)
            {
                // Write your code here.
                // Move row down
                XPathNavigator oRow = e.Source;
                XPathNavigator oRows = oRow.SelectSingleNode("..");
                XPathNavigator oNextRow = oRow.SelectSingleNode("following-sibling::my:Export_CB_Step_Details_Section", NamespaceManager);
                if (oNextRow != null)
                {
                    oRow.InsertAfter(oNextRow);
                    oNextRow.InsertAfter(oRow);
                    oRow.DeleteSelf();
                    oNextRow.DeleteSelf();
                }
            }

     As far as the other code you provided, I will give it a try.

    Thanks again!

     

Page 1 of 1 (6 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.