VB or C# to add Repeating Section - InfoPath Dev
in

InfoPath Dev

VB or C# to add Repeating Section

Last post 10-15-2014 03:15 PM by Marshall18. 21 replies.
Page 1 of 2 (22 items) 1 2 Next >
Sort Posts: Previous Next
  • 07-02-2013 08:22 PM

    VB or C# to add Repeating Section

    I have been searching for an answer that works and can't seem to get it going.

    Infopath 2010

    I have a repeating section and i want users to be able to add new sections but not remove them. I know that you can remove the delete option but you cant get rid of the cut feature. So the only way i can figure to get it working is to disable the ability for a user to control the section via the properties and add a button with some VB or C#. This will be the first code on the form and the first time i have had to use code with infopath. im not a stranger to VBA but i cannot seem to get any code suggested elsewhere to work.

    Examples of code:

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

    I was so happy when i found this article because it described exactly what i needed. to bad i could not get it working in 2010, even when i set the language to 2007 compatible VB / C#.

    I have attahced the test file that i have been trying to get working.

    Thanks in advance!

  • 07-03-2013 07:35 AM In reply to

    Re: VB or C# to add Repeating Section

    What's the code you've tried?

    Hilary Stoupa

  • 07-03-2013 09:30 AM In reply to

    Re: VB or C# to add Repeating Section

    I tried to use the code from this post:

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

    On my form i set the Form Options > Programming > Code Language to: Visual Basic (InfoPath 2007 Compatable)

    Then i went into the Button Properties > Edit Form Code > and paisted the Visual Basic 2007 example from the link above:

    CurrentView.ExecuteAction(ActionType.XCollectionInsert, "XmlToEdit") - I changed the "XmlToEdit" as described in the article.

    In Visual Studio is indicates that: 'ExecuteAction' is not a member of 'Microsoft.Office.Infopath.View'  and Name 'ActionType' is not declared.

    If i try to preview the form i get "There were build errors..."

    I removed code and tried the same process with the C# 2007 example and got the same kind of result.

    Then i tried to use code from this post:

    http://social.technet.microsoft.com/Forums/sharepoint/en-US/f80d9557-bd97-4333-8637-592f2d7a9475/infopath-add-a-new-row-to-the-middle-of-a-repeating-section-using-c

    MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:Test_Section",NamespaceManager).AppendChild(doc.DocumentElement.CreateNavigator());

    I am not sure if i changed "/my:myFields/my:Test_Section" correctly but regardless the 'doc' at the end was throwing an error saying that 'doc' does not exist in the cirrent context.

    Thanks again for your help.

  • 07-03-2013 03:34 PM In reply to

    Re: VB or C# to add Repeating Section

    I have been trying to find something that will work here...

    I have this C# code:

    XmlDocument doc = new XmlDocument();
                XmlNode newParent = doc.SelectSingleNode("Group2_1");
                XmlNode newScreenToSupport = newParent.Clone();
                newParent.ParentNode.AppendChild(newScreenToSupport);

    and im getting this error when i click the button:

    System.NullReferenceException
    Object reference not set to an instance of an object.
       at programmatically_add_new_section.FormCode.CTRL18_5_Clicked(Object sender, ClickedEventArgs e)
       at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)
       at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)

     

     

    I also tried:

    public void CTRL18_5_Clicked(object sender, ClickedEventArgs e)

    {

    XmlDocument doc = new XmlDocument();

    XmlNode newParent = doc.SelectSingleNode(sender);

    XmlNode newScreenToSupport = newParent.Clone();

    newParent.ParentNode.AppendChild(newScreenToSupport);

    }

     

    Once again, thank you for any help

  • 07-03-2013 03:38 PM In reply to

    Re: VB or C# to add Repeating Section

     Is your form a browser or filler form? If browser, ExecuteAction won't work for you.

     EDITED TO ADD - sorry, that didn't sound too helpful. But if ExecuteAction will work for you (your form will be a filler form) then it is going to be the simplest code - if not, I can help you with the code.

    Hilary Stoupa

  • 07-03-2013 04:05 PM In reply to

    Re: VB or C# to add Repeating Section

    Thanks for the responce!

    It is a browser form so that explains why that option is not working. Now, I made the form with the intention of using it in the browser but that is not a neccesity. I could switch the form over to a filler form if that makes it work.

     Any help is greatly appreciated.

     

  • 07-03-2013 04:06 PM In reply to

    Re: VB or C# to add Repeating Section

     If you change the form compatibility to Filler (under the form options) the initial code you were trying would likely work for you - and that would be the simplest approach. :)

    Hilary Stoupa

  • 07-03-2013 04:29 PM In reply to

    Re: VB or C# to add Repeating Section

    Well that sounds good. Is there anything i should lookout for when switching the compatability? any adverse effects?

  • 07-03-2013 04:32 PM In reply to

    Re: VB or C# to add Repeating Section

     Not when going from Browser to Filler - Filler has more functionality than Browser. The other direction you can have issues - but the Design Checker will usually give you a head's up.

    Hilary Stoupa

  • 07-03-2013 05:01 PM In reply to

    Re: VB or C# to add Repeating Section

     Well, it worked until i unchecked: 'Allow users to insert and delete sections'

    When i did that the XmlToEdit on the advanced tab goes away....

    I need to uncheck the 'Allow users to...' because the point of the exercise is to stop users from being able to remove sections. 

    Any advise on how i should proceed?

     

    Thanks again. 

  • 07-03-2013 05:05 PM In reply to

    Re: VB or C# to add Repeating Section

     okay - so you have to allow that in order for this technique to work - I'll grab your earlier form sample and help you with a little code.

    Hilary Stoupa

  • 07-03-2013 05:16 PM In reply to

    Re: VB or C# to add Repeating Section

     Okay - a few things I should mention - qRules can do this for you in a rules based way, as well as delete nodes and a kajillion other things - you can try a free download here.

    However, given the sample form you first posted, you could add code to the button on the form, and use this:

       //get the parent node for the repeating group
       XPathNavigator parent = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:A", NamespaceManager);

       //clone a node
       XPathNavigator child = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:A/my:Aa", NamespaceManager).Clone();

        //append the node
        parent.AppendChild(child);

    This is C#, for IP 2007 or greater. The stuff in bold you can replace with the correct XPath to your parent (non repeating) group and your child (repeating group). This is not bullet proof by any means - if, for example, you set your form to not include the repeating group by default, this will fail. But hopefully it is simple and understandable and you'll be able to try it.

    Hilary Stoupa

  • 07-03-2013 06:00 PM In reply to

    Re: VB or C# to add Repeating Section

    Fantastic! That worked.

    I do really want to mess around with qRules. I have seen it as the answer to many problems on the forums but it is not a part of my budget. I will however download the free version to learn more about it for future projects.

    Thank you for all of your help!!

  • 07-05-2013 11:23 AM In reply to

    Re: VB or C# to add Repeating Section

    In testing I realised that this will keep the data from the cloned node in the appended node. Is there any way to only clone the structure and not the data?

     Thanks again.

  • 07-05-2013 11:37 AM In reply to

    Re: VB or C# to add Repeating Section

     You have two choices - you can clear each field or you can clone from the template's sample data. I usually just clone from sampledata - that way even if the group isn't included by default, I'll be able to clone:

                //get the parent node for the repeating group
                XPathNavigator parent = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:A", NamespaceManager);

                //clone a node from sample data
                XmlDocument sampleData = new XmlDocument();
                sampleData.Load(this.Template.OpenFileFromPackage("sampledata.xml"));

                XPathNavigator child = sampleData.CreateNavigator().SelectSingleNode("/my:myFields/my:A/my:Aa", NamespaceManager).Clone();

                //append the node
                parent.AppendChild(child);

    Hilary Stoupa

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