Search code examples
tridiontridion2009

In a C# TBB: how to split a multi SingleLineTextField into seperate strings


I have a plain textfield in Tridion that can have multiple values. The itemtype is a SingleLineTextField.

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

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 case of two entries is just two strings with a character line break in it.

String A
String B

The method used is a generic one, which also works on other fields, so I was looking for some way to find out if a SingleLineTextField has more values in it.


Solution

  • You can cast the field to a SingleLineTextField type, then iterate through the Values collection, something along 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;