We help IT Professionals succeed at work.

Populate Repeating Section with Web Service Data

Beircheart
Beircheart used Ask the Experts™
on
Hi all,

I have an Infopath form that is used to gather information on New University Programmes and submit it to a database. This is working fine.
The form is also required to update existing programmes. So once the user selects a programme, I query a web service and this returns the required info in XML format.

I now need to populate a repeating section in the Programme form from the XML. Here is an example of the XML data that I am trying to use:
...
<ContributingDepts>
  <PRG_CODE>BAECSOUFA</PRG_CODE>
  <DEPT>Sociology</DEPT>
  </ContributingDepts>
- <ContributingDepts>
  <PRG_CODE>BAECSOUFA</PRG_CODE>
  <DEPT>Economics</DEPT>
  </ContributingDepts>
- <ContributingDepts>
  <PRG_CODE>BAECSOUFA</PRG_CODE>
  <DEPT>Mathematics and Statistics</DEPT>
  </ContributingDepts>
- <ContributingDepts>
  <PRG_CODE>BAECSOUFA</PRG_CODE>
  <DEPT>Psychology</DEPT>
  </ContributingDepts>
- <ContributingDepts>
  <PRG_CODE>BAECSOUFA</PRG_CODE>
  <DEPT>Management and Marketing</DEPT>
  </ContributingDepts>
...

How do I populate a repeating section with all the <DEPT>s for example? I can populate it sucessully if I "Change Binding" to the secondary data source (Web service) but this is not really an option as there is a complex workflow that is out of my hands already attached and changing bindings would destroy it.

If I try to populate field within the Repeating Section directly, it only shows the last one (i.e. Keeps overwriting rather than inserting new rows)

Any ideas?

Thanks a million,

Ben
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Hi,
I've had a look at my XML. Is it structured incorrectly. Rather than this:
...
<ContributingDepts>
  <PRG_CODE>BAECSOUFA</PRG_CODE>
  <DEPT>Sociology</DEPT>
  </ContributingDepts>
- <ContributingDepts>
  <PRG_CODE>BAECSOUFA</PRG_CODE>
  <DEPT>Economics</DEPT>
  </ContributingDepts>
- <ContributingDepts>
  <PRG_CODE>BAECSOUFA</PRG_CODE>
  <DEPT>Mathematics and Statistics</DEPT>
  </ContributingDepts>
- <ContributingDepts>
  <PRG_CODE>BAECSOUFA</PRG_CODE>
  <DEPT>Psychology</DEPT>
  </ContributingDepts>
- <ContributingDepts>
  <PRG_CODE>BAECSOUFA</PRG_CODE>
  <DEPT>Management and Marketing</DEPT>
  </ContributingDepts>
...

Should it be:
...
<ContributingDepts>
  <PRG_CODE>BAECSOUFA</PRG_CODE>
  <DEPT>Sociology</DEPT>
  <DEPT>Economics</DEPT>
  <DEPT>Mathematics and Statistics</DEPT>  
  <DEPT>Psychology</DEPT>
  <DEPT>Management and Marketing</DEPT>
</ContributingDepts>
...

I think if it was structured like so, it would populate the XML form correctly. Any thoughts?

I am generating this from a web service that I wrote and I'll alter it if necessary. Do you want the web service code I'm using to generate the XML document?

Author

Commented:
I've been doing a bit of testing with a dummy XML file and it seems that the XML structure is not the problem...

Also, not that it matters very much but I mentioned above:

"If I try to populate field within the Repeating Section directly, it only shows the last one (i.e. Keeps overwriting rather than inserting new rows)"

Actually it's inserting just the first one. (It had been inserting 'Sociology' and I thought they were in alphabetical order)
Most Valuable Expert 2011
Top Expert 2016

Commented:
Ben: If you have PHP available and you want to see how PHP handles XML, ask a moderator to add this to the PHP zone and I will help from that perspective.  PHP has a SimpleXML class that might be just what you need.  XML is not "ordered" as in alphabetical order - it is a markup language and the data sequence is generally unimportant or at least unrelated to the contents of the objects.  Good luck with it, ~Ray

Author

Commented:
Hi Ray. Thanks for your reply but PHP is not involved here. I've written the web service in VB.Net and I don't think the web service or how the outputted XML is structured is the problem.
I know I suggessted it in my 2nd post but I then retracted it in my 3rd.

As for the order, I understand that XML wouldn't be sorting the data. I was just saying that I thought that it was the last row that was being written to the drop down list box in Infopath .i.e. I thought the data kept on writing over the content of the single field until it ran out of rows rather than creating a new row in the repeating table each time.

However, I was wrong. It was actually the first row that was being inserted and no more rows appeared to even make an attempt to insert themselves in to the repeating table in my Infopath form.

That got a bit hard to explain so my apologies if I've just confused things.

Thanks all the same Ray

Bottom line: What I need to do is populate a repeating section in an Infopath form using Xml data.
Hi all,

Problem solved. I ended up writing the following code in the Infopath form to populate the repeating table programmatically:

See Code Snippet

All the best,

Ben
ClearRepeatingTable("/my:ProgrammeForm/my:Descriptors/my:AssociatedFacultiesSection/my:AssociatedFacultiesGroup/my:AssociatedFaculties")
            PopulateRepeatingTable("ProgrammeInfo", "/dfs:myFields/dfs:dataFields/tns:ProgrammeInfoResponse/tns:ProgrammeInfoResult/Programme/AssocFaculty", "FAC", _
                                   "/my:ProgrammeForm/my:Descriptors/my:AssociatedFacultiesSection/my:AssociatedFacultiesGroup", "AssociatedFaculties", "AssociatedFacultiesList")

        End Sub

        Sub PopulateRepeatingTable(ByVal DataSource As String, ByVal SourceXPath As String, ByVal SourceFieldName As String, _
                                   ByVal DestXPath As String, ByVal DestGroup As String, ByVal DestTable As String)

            Dim NodeIterator As XPathNodeIterator = DataSources(DataSource).CreateNavigator().Select(SourceXPath, NamespaceManager)

            For Each Node As XPathNavigator In NodeIterator

                Dim MyNamespace As String = NamespaceManager.LookupNamespace("my")

                If Node.SelectSingleNode(SourceFieldName) Is Nothing Then
                    'Do Nothing
                Else
                    Using Writer As XmlWriter = MainDataSource.CreateNavigator().SelectSingleNode(DestXPath, NamespaceManager).AppendChild()

                        Writer.WriteStartElement(DestGroup, MyNamespace)
                        Writer.WriteElementString(DestTable, MyNamespace, Node.SelectSingleNode(SourceFieldName).ToString)
                        Writer.WriteEndElement()
                        Writer.Close()

                    End Using
                End If

            Next

        End Sub

        Sub ClearRepeatingTable(ByVal TableXPath As String)

            Dim Root As XPathNavigator = MainDataSource.CreateNavigator()
            Dim Iter As XPathNodeIterator = Root.Select(TableXPath, NamespaceManager)
            Dim NodeCount As Integer = Iter.Count

            If NodeCount > 0 Then

                Dim FirstNode As XPathNavigator = Root.SelectSingleNode(TableXPath + "[1]", NamespaceManager)
                Dim LastNode As XPathNavigator = Root.SelectSingleNode(TableXPath + "[" + NodeCount.ToString() + "]", NamespaceManager)

                FirstNode.DeleteRange(LastNode)

            End If

        End Sub

Open in new window