Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan Diplo 1545 posts 6118 karma points MVP 4x c-trib
    Sep 10, 2012 @ 12:27
    Dan Diplo
    0

    Programmatically populate TextString Array with (XML) data

    I'd like to be able to pre-populate some fields of the TextString Array datatype when a document is first created in Document_BeforePublish event (in ApplicationBase). However, I'm not sure what format the data should be created. For instance, say I have a property called arrayData then how would I save the XML back to that property programatically?

    I tried copying some XML for a previously created TextString Array from umbraco.config and then saving it back to the property (in the before publish event) as a string, but this threw an "Invalid JSON primitive" error. Any ideas? Can it be saved as a string or does it need to be an XmlNode or something?

  • Lee Kelleher 3987 posts 15554 karma points MVP 11x admin c-trib
    Sep 10, 2012 @ 12:31
    Lee Kelleher
    1

    Hi Dan,

    The raw data is stored as a JSON two-dimensional string array (serialized), e.g.

    [ ["cell-1-1", "cell-1-2"], ["cell-2-1", "cell-2-2"], ["cell-3-1", "cell-3-2"] ]

    It was simpler to keep the JSON/JavaScript format for use in the back-office. The XML respresentation of that is generated at publish time, (for XSLT, Razor, etc).

    Cheers, Lee.

  • Dan Diplo 1545 posts 6118 karma points MVP 4x c-trib
    Sep 10, 2012 @ 12:33
    Dan Diplo
    0

    Wow! That was a fast response, Lee - awesome. I should be able to work with that.

    So I just save the serialized JSON as a string to the property, then?

  • Lee Kelleher 3987 posts 15554 karma points MVP 11x admin c-trib
    Sep 10, 2012 @ 12:36
    Lee Kelleher
    0

    Yes, that will work.

    There has been some confusions about which format to store the raw data (for data-types) in the database... as Umbraco can transform that data at publish time - which is when it's convered to XML.  (Some data-types persist the XML back to the database - it's all based on the developer's preference)

    Cheers, Lee.

  • Dan Diplo 1545 posts 6118 karma points MVP 4x c-trib
    Sep 10, 2012 @ 12:53
    Dan Diplo
    1

    Brilliant! You learn something every day :)

    I actually found you needed to create a "jagged" string array to serialise correctly to JSON, but other than that it worked fine. Example code below for anyone else who might need it (assuming you have 2 rows and 8 columns):

    var data = new string[][]
    {
        new string[] {"1.1","1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8"},
        new string[] {"2.1","2.2", "2.3", "2.4", "2.5", "2.6", "2.7", "2.8"},
    };
    
    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    string json = serializer.Serialize(data);
    
    return json;
    

     

  • Lee Kelleher 3987 posts 15554 karma points MVP 11x admin c-trib
    Sep 10, 2012 @ 12:56
    Lee Kelleher
    0

    Ace!

    We're going to be working on new documentation soon (hosted on GitHub), which we'd like to show more examples like this. Thanks.

    Cheers, Lee.

  • Chriztian Steinmeier 2737 posts 8401 karma points MVP 5x admin c-trib
    Sep 10, 2012 @ 13:13
    Chriztian Steinmeier
    0

    Docs on GitHub == Awesome !!!

    /Chriztian

  • Jan Vermeerbergen 79 posts 112 karma points
    Mar 12, 2013 @ 21:02
    Jan Vermeerbergen
    0

    Hi Lee,

    I'm fairly new to razor, and am trying to implement your input above.

    I can't get my head around how to append (instead of change the value altogether) a new row to the textstring-array (which has 4 columns)?

    Any suggestions would be very much appreciated !
    Jan 

Please Sign in or register to post replies

Write your reply to:

Draft