InfoPath how to copy a repeating section using rules

For years, we all thought this was impossible.  You had to use code.  I somehow woke up with an idea on how to do this, and set about testing and to my surprise, found a solution.  Here it is!

 

The form set up

image

  • Two repeating sections, Foo's and Bar's
  • One additional integer field i to control which row to copy

Put the elements on the form:

image

 

 

The copy rule

Create a rule on the integer field i

  1. We will use the normal action, then tweak it. 
    Give this rule the name "Copy"
    Start by defining a set field value rule.
    image
  2. Set /my:myFields/my:Foos/my:Foo/my:F1 to /my:myFields/my:Bars/my:Bar/my:B1
    image

    There are a few issues so far with the default Set Field Value action related to a repeating section.
    • For the Field (target), it will set ALL the matching nodes.
    • For the Value, it will get the First matching node.
  3. Let's fix the second value - we can do this in InfoPath designer.
    image

    The expression is:
    ../my:Bars/my:Bar[number(../../my:i)]/my:B1

    This expression lets us copy a different row.

    image

    So we can now copy value from any row - depending on what the number i is.
  4. To fix the Field Target is a bit more difficult.  InfoPath designer doesn't give us a way to modify the XPath of the field.

  5. Save the form.  We're about to do unsupported stuff. 
  6. Publish the form to Source Files. 
    image

    I publish to C:\Temp\CopyForm\
  7. Close InfoPath designer.  Open the file C:\Temp\CopyForm\manifest.xsf file using NotePad or your favourite XML editor.
  8. The "Copy" rule is hiding in this XML file, it looks like this:
  9. <xsf:rule caption="Copy">
        <xsf:assignmentAction targetField="../my:Foos/my:Foo/my:F1" expression="../my:Bars/my:Bar[number(../../my:i)]/my:B1"></xsf:assignmentAction>
    </xsf:rule>

  10. Change it to:
  11. <xsf:rule caption="Copy">
    <xsf:assignmentAction targetField="../my:Foos/my:Foo[number(../../my:i)]/my:F1" expression="../my:Bars/my:Bar[number(../../my:i)]/my:B1"></xsf:assignmentAction>
    </xsf:rule>
  12. Save, and close Notepad.  Re-open manifest.xsf file using InfoPath Designer
  13. Let's check our rule.
    image
  14. Are you thinking what I'm thinking?  We're nearly there.

The loop

  1. This rule is set on the number i, and runs once whenever the number i changes.
  2. Lets set it up to increment.
    image
  3. Add a condition to stop incrementing when we've run out of rows
    image

    image
  4. And to start the whole thing, remember we have a button.

    image

    To start the process, set i to 1.  XML index starts from 1 to n.  Does not start at 0.

 

Result - InfoPath Filler


image
Starting...

image

Press "Copy"

Result - Form Server:

image

Starting Browser Form

image

Copy.

 

Note - InfoPath "Infinite Loop" limitation:

InfoPath is hard coded to only execute 16 rules before throwing the "Infinite Loop" error.

image

An error occurred in the form's rules or code. The number of rule actions or the number of calls to the OnAfterChange event for a single update in the data exceeded the maximum limit.

The rules or code may be causing an infinite loop. To prevent this, ensure that the rule action or event handler does not update the data which causes the same rule action or event handler to execute.

 

There is no solution for hard coded 16 rules.  So your i can not go over 16.

Download

InfoPath - LoadDocumentAndPlayEventLog NullReferenceException

Error:

LoadDocumentAndPlayEventLog failed with unhandled exception System.NullReferenceException: Object reference not set to an instance of an object.     at Microsoft.Office.InfoPath.Server.Converter.DetectUnsupportedNamespaces.VerifyNamespace

This is a very uncommon bug.  Essentially, you are using custom code in InfoPath, and you are using code that doesn't have a namespace.  Say a helper function that you've included in your code.

When InfoPath Forms Services attempt to validate your form, it finds that your form template contains reference to code that doesn't have a namespace!

The fix is simple, create a namespace for the helper class, or move the helper class under the namespace of the form's namespace.

InfoPath - check leap year using expression

 

This is a fun one.

  1. Use the undocumented msxsl:utc function, which checks / converts a date into a normalized representation.

    image

  2. So, what will happen if I test it with with a garbage date, say, the 29th of February of 2001, which isn't a leap year?

    image
  3. So just check if the result of the utc function returns empty string or not.

Expression

msxsl:utc(concat(my:year, "-02-29")) != ""

InfoPath - removing newline (CRLF) using rules

 

  1. In InfoPath (and in Windows in general), a newline is two characters \r\n (carriage return, followed by a newline character).
  2. You can't easily use \r or \n within InfoPath rules, since the XML is escaped into \\r and \\n defeating your intention.
  3. First, add a characters secondary data source: Add an external XML file for characters
    You should get this secondary data source with the special characters we need.
    image
  4. On the field that you want to remove newlines, add a changed rule:
    image

    The translate rule is:
    translate(., @crlf, "")
    or expanded xpath:
    translate(., xdXDocument:GetDOM("characters")/lookups/characters/@crlf, "")

    use select field and change the datasource to characters.
  5. To prevent this rule from running into infinite loop, set a condition:

    image

    The expression is:
    contains(., xdXDocument:GetDOM("characters")/lookups/characters/@crlf)
  6. Result:
    image 

Test it!


image
Test typing something with newline

image
Rule runs and removes any new line characters

InfoPath - Concat SharePoint list with the Eval function (aka Voodoo)

 

It really can't hurt to play with the crazy XPath capabilities within InfoPath.  This is documented in many places, starting:

http://blogs.msdn.com/b/infopath/archive/2006/04/05/569338.aspx

Which offered possibly the best explanation of how this technique with Eval() actually works.  I prefer to remembered this as PURE VOODOO.

 

1. Create our SharePoint list

image

 

2. Create a secondary data connection to the list

image

image

 

3. Drag the dataFields section into the form, to create a common binding parent

image

This step makes step 4 a lot easier, since the Eval loop is relative and works on the repeating section inside the dataFields section.

 

4. Add expression box within this Section

image

The Expression is:
xdMath:Eval(xdMath:Eval(d:SharePointListItem_RW, 'concat(d:Title, ":")'), "..")

 

5. Result:

image