InfoPath 2007 Customized Code – Insert/Remove a Repeating Section

One of our customer want to customized the InfoPath 2007 repeater looks like the following, here is the code to achieve this.

Code behind the “AddRepeatRow” button in VB.NET:

<InfoPathEventHandler(MatchPath:="AddRepeatRow", EventType:=InfoPathEventType.OnClick)> _
Public Sub AddRepeatRow_OnClick(ByVal e As DocActionEvent)
    Dim RepeatSection As IXMLDOMNode = GetNodeByName(thisXDocument.DOM, "RepeatSection")
    Dim RepeatRow As IXMLDOMNode = GetNodeByName(GetTemplateXMLFileDOM, _
              "RepeatRowSection").cloneNode(True)
    RepeatSection.appendChild(RepeatRow)
End Sub

 

Code behind the “RemoveRow” button:

<InfoPathEventHandler(MatchPath:="RemoveRow", EventType:=InfoPathEventType.OnClick)> _
Public Sub AssessmentDateTimeRemove_OnClick(ByVal e As DocActionEvent)
    Dim AssessmentDateTimeRemoveNode As IXMLDOMNode = e.Source
    e.Source.parentNode.removeChild(AssessmentDateTimeRemoveNode)
End Sub

 

Code behind the “RemoveMultiRow” button:

<InfoPathEventHandler(MatchPath:="RemoveMultiRow", EventType:=InfoPathEventType.OnClick)> _
Public Sub RemoveRow_OnClick(ByVal e As DocActionEvent)
    Dim group1Section As IXMLDOMNode = GetNodeByName(thisXDocument.DOM, "RepeatSection")
    Dim numbers As Integer = group1Section.childNodes.length 'how many child node in group1
    Dim number As Integer = 1
    While number <= numbers
        Dim group2SectionSingle As IXMLDOMNode = group1Section.selectSingleNode("my: 
              RepeatRowSection[" & number.ToString() & "]")
        If group2SectionSingle.selectSingleNode("my:DeleteRow") IsNot Nothing Then
             Dim group2SectionSingleDeleteTrue As Boolean = 
                group2SectionSingle.selectSingleNode("my:DeleteRow").text
             If group2SectionSingleDeleteTrue = True Then
                 group1Section.removeChild(group2SectionSingle)
                 're-calculate the number, because 1 node has been deleted
                 numbers = numbers - 1
                 number = number - 1
             End If
          End If
          number = number + 1
     End While
End Sub

 

Code behind the “AddMoreRow” button that has the index number shows in the “RepeaterSectionRowNumber” field:

<InfoPathEventHandler(MatchPath:="AddMoreRow",EventType:=InfoPathEventType.OnClick)> _
Public Sub AddMoreOccupationalGoals_OnClick(ByVal e As DocActionEvent)
    Dim nRepeaterSection As IXMLDOMNode = GetNodeByName(thisXDocument.DOM, "RepeaterSection")
    Dim number As Integer = nRepeaterSection.childNodes.length + 1
    Dim nRepeaterSectionRow As IXMLDOMNode = GetNodeByName(objShared.GetTemplateXMLFileDOM, "RepeaterSectionRow").cloneNode(True)
    SetXMLNodeValue(nRepeaterSectionRow.selectSingleNode("my:RepeaterSectionRowNumber"), number.ToString(), False)
    nRepeaterSection.appendChild(nRepeaterSectionRow)
End Sub

 

Other functions used (C# code):

public static IXMLDOMNode GetNodeByName(ref IXMLDOMNode StartNode, string NodeName, bool 
       IgnoreMYStructure = false)
{
      IXMLDOMNode xmldomNode = (IXMLDOMNode) null;
      if (!IgnoreMYStructure)
          NodeName = ConvertToMYStructure(NodeName);
      try
      {
          xmldomNode = StartNode.selectSingleNode(NodeName);
      }
      catch (Exception ex)
      {
          ProjectData.SetProjectError(ex);
          ProjectData.ClearProjectError();
      }
      if (Information.IsNothing((object) xmldomNode))
      {
          try
          {
              xmldomNode = StartNode.selectSingleNode("my:myFields/" + NodeName);
          }
          catch (Exception ex)
          {
              ProjectData.SetProjectError(ex);
              ProjectData.ClearProjectError();
          }
      }
      if (Information.IsNothing((object) xmldomNode))
      {
        int num1 = 0;
        int num2 = checked (StartNode.childNodes.length - 1);
        int index = num1;
        while (index <= num2)
        {
          if (Operators.CompareString(StartNode.childNodes[index].nodeName, NodeName, false)
               == 0)
              return StartNode.childNodes[index];
          if (StartNode.childNodes[index].hasChildNodes())
          {
              IXMLDOMNode childNode = StartNode.childNodes[index];
              xmldomNode = GetNodeByName(ref childNode, NodeName, IgnoreMYStructure);
              if (!Information.IsNothing((object) xmldomNode))
                      return xmldomNode;
          }
          checked { ++index; }
        }
      }
      return xmldomNode;
} 

public static string ConvertToMYStructure(string CurrentName)
{
      if (CompilerServices.Operators.CompareString(CurrentName, "", false) != 0)
      {
           CurrentName = Strings.Replace(CurrentName, "/", "/my:", 1, -1,CompareMethod.Binary);
           CurrentName = Strings.Replace(CurrentName, "/my:my:", "/my:", 1, -1,
                   CompareMethod.Binary);
           if (Operators.CompareString(Strings.Left(CurrentName, 1), "/", false) !=0 &&
               Operators.CompareString(Strings.Left(CurrentName, 3), "my:", false) != 0)
           {
              CurrentName = "my:" + CurrentName;
              CurrentName = Strings.Replace(CurrentName, "my:my:", "my:", 1, -1, 
                       CompareMethod.Binary);
           }
      }
      return CurrentName;
}

private XDocument prXDoc;
private System.Type prParentClassType;
private string prInfoPathNamespace;

public IXMLDOMDocument3 GetTemplateXMLFileDOM(string strTemplateName = "template.xml")
{
      IXMLDOMDocument3 dom = (IXMLDOMDocument3) this.prXDoc.CreateDOM();
      dom.async = false;
      dom.validateOnParse = false;
      dom.load((object) strTemplateName);
      this.prInfoPathNamespace = ((InfoPathNamespaceAttribute)
          Attribute.GetCustomAttribute((MemberInfo) this.prParentClassType,
          typeof (InfoPathNamespaceAttribute))).UserNamespace;
      dom.setProperty("SelectionNamespaces", (object) this.prInfoPathNamespace);
      return dom;
}

 

Advertisements

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