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?
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).
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)
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;
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?
Hi Dan,
The raw data is stored as a JSON two-dimensional string array (serialized), e.g.
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.
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?
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.
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):
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.
Docs on GitHub == Awesome !!!
/Chriztian
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
is working on a reply...