In C # TBB: how to split multiple SingleLineTextField into separate lines

I have a regular text field in Tridion that can have multiple values. Itemtype - SingleLineTextField.

In the TBB code, I have the following (non-essential parts removed):

ItemFields itemFields = new ItemFields(folder.Metadata, folder.MetadataSchema); foreach (ItemField itemField in itemFields) { string itemFieldValue = string.Empty; switch (Utilities.GetFieldType(itemField)) { case FieldType.SingleLineTextField: itemFieldValue = itemField.ToString(); break; } } 

Now the result in the case of two entries is just two lines with a character line break.

 String A String B 

The method used is general, which also works with other fields, so I was looking for a way to find out if SingleLineTextField has more values.

+4
source share
2 answers

You can apply the field to the SingleLineTextField type and then iterate over the collection of values, something like these lines:

 SingleLineTextField field = (SingleLineTextField)itemField; foreach(string value in field.Values) { // do something with value } // or if all you want is the count of values int i = field.Values.Count; 
+7
source

First, I would advise you not to rely on the ToString() method on objects if it is not documented. In this case, it works with the abstract class ItemField , but this may not always be the case.

The TOM.Net API only defines the definition and name properties for the ItemField , so you need to make the ItemField object more specific.

the abstract TextField class that inherits from SingleLineTextField defines the ToString() method, as well as the Value and Values properties, which are much better for what you are trying to do. After looking at the documentation, we can see that Values will give us IList<String> values, even if your field is not ambiguous. Excellent!

So, to answer your question: โ€œI was looking for some way to find out if SingleLineTextField has more values โ€‹โ€‹in itโ€, you need to specify your ItemField as TextField and check the number of values โ€‹โ€‹it provides, this way:

 TextField textField = (TextField)itemField; // If you need to deal with multi-valued fields separately if (textField.Values.Count > 1) { //Logic to deal with multiple values goes here } else { //Logic to deal with single valued goes here } // Much better... If you can deal with any number of values in a generic fashion foreach (string value in textField.Values) { // Generic code goes here } 
+4
source

All Articles