Wednesday, January 11, 2012

Populate the InfoPath 2007 Repeater sections with data from secondary data source


I had a requirement to populate the questions answer section into a InfoPath 2007 form at runtime, in which data will come from custom SharePoint list.

There were following fields for each question:
Question Text
CheckBox as answer
Textbox (If required)

Now after populating them we need to save all the answers into form. But here we are not sure how many questions will be there as they are auto populated from SharePoint List so we cannot save each answer into seperate field into form, and I used delimited text for this pupose.
vb.Net Code:

Code to popluate the questions at runtime:
[ Edit flag is used to control the repeater section insert, remove functions as these sections get populated at runtime so user will not have option to inset or remove any section. For this purpose I had to create a flag for a rule which will disable these functions when my flag is false ]

Dim editFlag As IXMLDOMNode = thisXDocument.DOM.selectSingleNode("/my:TMS/my:GeneralChecklistGroup/my:EditFlag")
editFlag.text = "1"

Dim xDocumentQA As IXMLDOMDocument2 = DirectCast(thisXDocument.GetDOM("Check List Questions"), IXMLDOMDocument2)

xDocumentQA.setProperty("SelectionNamespaces", "xmlns:dfs=""http://schemas.microsoft.com/office/infopath/2003/dataFormSolution""")

Dim nodeRoot As IXMLDOMNode = xDocumentQA.selectSingleNode("dfs:myFields")

If nodeRoot.childNodes(0).childNodes(0).attributes(1).text = "1" Then

myQAGroup.childNodes(1).childNodes(1).text = " " + nodeRoot.childNodes(0).childNodes(0).attributes(0).text 'Text;

Else

myQAGroup.childNodes(1).childNodes(1).text = nodeRoot.childNodes(0).childNodes(0).attributes(0).text 'Text;

End If

myQAGroup.childNodes(1).childNodes(5).text = nodeRoot.childNodes(0).childNodes(0).attributes(2).text 'TextBoxRequired

myQAGroup.childNodes[1].childNodes[11].text = nodeRoot.childNodes[0].childNodes[0].attributes[4].text 'ID

Dim intCtr As Integer = nodeRoot.childNodes(0).childNodes.length - 1

For i As Integer = 0 To nodeRoot.childNodes(0).childNodes.length - 2

Dim myQAInsertSection As IXMLDOMNode = thisXDocument.DOM.selectSingleNode("/my:TMS/my:GeneralChecklistGroup/my:Questions/my:QuestionsSection")

thisXDocument.View.SelectNodes(myQAInsertSection, Type.Missing, Type.Missing)

thisXDocument.View.ExecuteAction("xCollection::insert", "QuestionsSection_140")

If nodeRoot.childNodes(0).childNodes(intCtr).attributes(1).text = "1" Then 'IsOption

myQAGroup.childNodes(3).childNodes(1).text = " " + nodeRoot.childNodes(0).childNodes(intCtr).attributes(0).text 'Text

Else

myQAGroup.childNodes(3).childNodes(1).text = nodeRoot.childNodes(0).childNodes(intCtr).attributes(0).text 'Text

End If

myQAGroup.childNodes(3).childNodes(5).text = nodeRoot.childNodes(0).childNodes(intCtr).attributes(2).text 'TextBoxRequired

Iterate through InfoPath 2007 Repeater Table to get the values Programatically

I had a task to iterate through the repeater table of InfoPath 2007 form and get all the values entered by user on submit.

Here is a vb.net code

Imports Microsoft.Office.InfoPath

'' ********** Upload multiple attachments **************
Public Sub btnUploadAllAttachments_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)

Dim xPathNavigator As XPathNavigator = MainDataSource.CreateNavigator()

Dim rows As XPathNodeIterator = xPathNavigator.Select("/my:ShowCauseForm/my:ShowCauseDetails/my:AttchmentsTable/my:AttachmentRow", NamespaceManager)

While rows.MoveNext()

Upload(rows.Current.SelectSingleNode("my:Attachment", NamespaceManager).Value, "/my:ShowCauseForm/my:ShowCauseDetails/my:HyperLinkSection/my:AttachmentUrl", "Attachment")
End While

Windows.Forms.MessageBox.Show("All attachments uploaded successfully", "Attachments Upload", MessageBoxButtons.OK)

End Sub

How to check if there is any record in the data source for infopath rule

While develpoing InfoPath 2007 form there was a requirement to reset naming of forms at the change of year i.e. - eg. 12-01

Now we can do this by using couple of fields and get maximum no from data submitted document library. But issue is when we need to create a very first form into that published document library.

So while getting the maximum no we need to check if there is any record in document Library or not. If not then start by 1

Solution is to have a field in the document library to save the form no and get get max form no when new form loads and save into another field.

If max form no is blank then reset it from 1 else increment the no.