10

I'm trying to map multiple address fields from my source (i.e. address 1, City, State, Zip) into a target field which is a multi-line textbox.

enter image description here

I'm using the OOTB "Collection to String Value" reader as the Source Value Transformer within the address mapping.

enter image description here

However, it appears my delimiter of \r\n is being encoded somehow, so instead of line breaks i get

Address1\r\n\City\r\nState\r\nZip

Any way I can map multiple fields into separate lines in a multi-line textbox?

I probably need to write my own reader to transform the value - but I wanted to check if there was a better way first.

I'm also going through the framework code - and i don't see where the value is actually encoded - so i'm concerned if i DO write a custom value reader my value will just end up encoded as well - so i think i need to find that FIRST so i know how to avoid it

For example, The ootb value reader is only this

public virtual ReadResult Read(object source, DataAccessContext context)
{
  if (source == null)
    return ReadResult.PositiveResult((object) null, DateTime.Now);
  ICollection collection = source as ICollection;
  if (collection == null)
    return ReadResult.NegativeResult(DateTime.Now);
  List<object> objectList = new List<object>();
  foreach (object obj in (IEnumerable) collection)
    objectList.Add(obj);
  return ReadResult.PositiveResult((object) string.Join<object>(this.Separator, (IEnumerable<object>) objectList), DateTime.Now);
}

So it seems that simply writing my own value reader to add line breaks is pointless as unless I figure out how to avoid the encoding.

UPDATE -Solution from Vlad below was it. Below is the updated converter in case it helps others.

protected override ConvertResult<IValueReader> ConvertSupportedItem(ItemModel source)
{
    return this.PositiveResult((IValueReader)new CollectionToStringValueReader()
    {
            Separator = PreserveLineBreaks(this.GetStringValue(source, FieldNameSeparator))
    });
 }

private string PreserveLineBreaks(string separator)
{
    var unescapedSeparator = Regex.Unescape(separator);

    return unescapedSeparator == Environment.NewLine ? unescapedSeparator : separator;
}
2
  • I am not sure but as far as I know, Multi-Line Text field cannot does not convert the \r\n as a break line. You might need to change the field type to Rich Text or add a custom Multi-Line Text field Commented Feb 13, 2018 at 14:38
  • It's encoded in CollectionToStringValueConverter. Converter reads data from field and its value "\\r\\n".
    – Vlad Shpak
    Commented Feb 13, 2018 at 16:05

1 Answer 1

8

When converter gets separator value from field its value is "\\r\\n" and at the end your result is "Address1\r\n\City\r\nState\r\nZip".

Just add your custom converter.

    [SupportedIds("[YOUR TEMPLATE ID]")]
public class CollectionToStringNewLIneConverter : BaseItemModelConverter<IValueReader>
{
    public CollectionToStringNewLIneConverter(IItemModelRepository repository) : base(repository)
    {
    }
    protected override ConvertResult<IValueReader> ConvertSupportedItem(ItemModel source)
    {
        var reader = new CollectionToStringValueReader
        {
            Separator = Environment.NewLine
        };
        return PositiveResult(reader);
    }
}

Or still use the same template with Separator field but with custom converter to enescape separator value.

[SupportedIds("{03639AB3-5C4B-45E3-AE4E-A41EF33F3E06}")]
public class CustomUnescapeCollectionToStringValueReaderConverter : BaseItemModelConverter<IValueReader>
{
    public const string FieldNameSeparator = "Separator";
    public CustomUnescapeCollectionToStringValueReaderConverter(IItemModelRepository repository) : base(repository)
    {
    }
    protected override ConvertResult<IValueReader> ConvertSupportedItem(ItemModel source)
    {
        var reader = new CollectionToStringValueReader
        {
            Separator = Regex.Unescape(this.GetStringValue(source, FieldNameSeparator))
        };
        return PositiveResult(reader);
    }
}
1
  • 1
    Thank you Vlad! That was it. FYI - only change I made to your suggestion was just to limit the unescaping to the linebreaks - to lessen the chance of unintended consequences. I'll edit the question to include the updated code.
    – trowpa
    Commented Feb 13, 2018 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.