Udostępnij za pośrednictwem


Inserting line breaks into text using Rules

Q: How do I insert line breaks into a multi-line text box using rules?

First off, let’s introduce the concept of a multi-line text box properly. This functionality was added in InfoPath 2003 SP1. To enable it, insert a Text Box control, then on the Display tab, check “Paragraph breaks”. I usually also set the scrolling property to “Show scrollbars when necessary” and turn on “wrap text” which maps to the usual behavior for a multi-line text box. I also usually make the control bigger.

Without the “Paragraph breaks” property set, line breaks (CR, LF or CRLF) in the XML data are treated as any other whitespace and show up in a text box as spaces. The Enter key is ignored, and breaks are stripped on paste. With this property set the breaks are preserved and show in the control, and can be typed and pasted.

That’s good enough for round-tripping pre-existing breaks in the XML data and allowing user editing. And since you can party on the DOM as much as you want, inserting them via code is easy too – use whatever the escaping mechanism your language of choice provides – for example, in JScript you might use: oNode.text = "abcrndef"

How about rules? One of the rule actions available is to set a field’s value to the result of an expression. Behind the scenes, when the rule fires the expression is evaluated to return a string, and then the string is injected into the XML DOM as the node’s text. Specifying expressions like concat("abc", "
", "def") will result in a literal “&”, “#”, “x”, “A” and “;” appearing in the XML text. &-encoding is part of the file format, not of the DOM! The expression concat("abc", "n", "def") and other variants don’t work either – there’s no escaping mechanism in the evaluation used here.

So you need to figuratively copy/paste the line break from another source. Start off by literally doing a copy/paste of the following text into Notepad:

<?xml version="1.0" encoding="UTF-8"?>
<characters
cr="&#xD;"
lf="&#xA;"
crlf="&#xD;&#xA;"
/>

Save this as a file called “characters.xml”. Then in InfoPath go to Tools | Data Connections and click Add. Select Receive data, then XML Document. Browse to characters.xml then complete the wizard. When it asks “The selected file is not part of the form...add this file...?” click Yes. At this point we’ve just added a resource file to the template that gets loaded into a declaratively-accessible DOM - no code required.

Almost there - now let’s use it!

Add a Button control, open up the properties and click Rules. Add a Rule, and add an Action of the type “Set a field’s value”. For the field pick the text box’s field (e.g. field1). For the new value, use the formula builder and build the following expression:

concat(field1, @crlf, "Hello, world!")

The underlines indicate that this is a simplified version of the expression that conceals the full XPaths. Instead of typing it's easier to pick the fields using the “Insert Field or Group” button. Pick field1 from the main data source and pick @crlf from the “characters (Secondary)” data source (using the drop-down at the top of the dialog). Behind the scenes (as you can tell by clicking the “Edit XPath” button) this builds up the following expression:

concat(my:field1, xdXDocument:GetDOM("characters")/characters/@crlf, "Hello, world!")

Once you're done, click Preview, and try clicking the button a few times.

As a final note, InfoPath is agnostic about the whole CR vs. LF vs. CRLF debate. Any of the three is equally treated as a line break.

Comments

  • Anonymous
    March 05, 2007
    Si bien esta característica la podemos utilizar en InfoPath 2003 SP1, en muchos escenarios particulares

  • Anonymous
    March 30, 2007
    Si bien esta caracter&shy;ística la podemos utilizar en InfoPath 2003 SP1, en muchos escenarios particulares

  • Anonymous
    May 01, 2007
    PingBack from http://www.kintespace.com/rasxlog/?p=621

  • Anonymous
    October 18, 2007
    In Part I and Part II I posted about the problem caused by the static size of the InfoPath Forms Services

  • Anonymous
    June 10, 2008
    I was able to insert a newline using a rule without adding a second data source.

  • add the rule's action as concat("abc", "&#xA;", "def")

  • save and close

  • rename the file as .cab

  • unpack the cab

  • open the manifext.xsf in a text editor, find your rule, and change it from &quot;&amp;#xA;&quot to &quot;&#xA;&quot

  • repack the cab

  • rename back to .xsn You can manage the cab packing/unpacking better than I describe here, but that's up to you to figure out.  This solution worked for me using the infopath form as a workflow task form in a custom .NET workflow for SharePoint.

  • Anonymous
    November 26, 2008
    PingBack from http://blog.12thwave.com/?p=18

  • Anonymous
    January 18, 2009
    The comment has been removed

  • Anonymous
    January 20, 2009
    PingBack from http://www.hilpers.com/1085764-zeilenumbruch-in-druckansicht

  • Anonymous
    January 21, 2009
    PingBack from http://www.keyongtech.com/1236618-concat-function

  • Anonymous
    August 26, 2009
    Should this work with a rich text field?  I've noticed that rich text fields show up when the form is emailed to the BlackBerry, so was going to make a rich text field using concat to sum up what needs summing up...

  • Anonymous
    April 27, 2010
    The comment has been removed

  • Anonymous
    November 18, 2010
    I try to use this example in mi form but I don't get the same result. What could be happening?, Please help me.

  • Anonymous
    November 18, 2010
    I try to use this example in mi form but I don't get the same result. What could be happening?, Please help me.

  • Anonymous
    June 21, 2011
    This works well for forms that open up in the client program but it does not work with web forms in InfoPath Forms Services. Does anyone know a way I can accomplish this? Thanks in advance.

  • Anonymous
    June 23, 2011
    The comment has been removed

  • Anonymous
    December 17, 2012
    The comment has been removed

  • Anonymous
    December 17, 2012
    The comment has been removed

  • Anonymous
    March 22, 2013
    The comment has been removed

  • Anonymous
    July 27, 2014
    This information was very usefull to me. Thank You

  • Anonymous
    January 30, 2015
    great post, thank you!!!

  • Anonymous
    May 04, 2015
    or just use CTRL+ENTER in Quotation marks "" ;0)