Accessing a short-text custom page field with API from page-node
This should be simple, but is driving me nuts
I've done this in the past for related Images and Videos, but I can't work it out for a Short-Text field.
How can I access the value of a Short-Text custom field on a Page, from the SiteMap CurrentNode ??
var node = SiteMapBase.GetActualCurrentNode();
if
(node ==
null
)
// Handle the non-existent page condition (Oops!)
else
// Get the related item (CustomField on the Page)
var item = ((IEnumerable<IDataItem>) node.GetCustomFieldValue(
"MY_TEXT_FIELD"
)).FirstOrDefault();
// Check the item
if
(item ==
null
)
// Handle the empty field condition
else
// Get the field value (as a string)
var value = item.????
Actually, I worked this out... just me being an idiot (as usual).
FWIW:
A text field can't have multiple references, so isn't stored as an enumerable dataItem (as you would with an image or video) ...so all you need to do is retrieve the fieldvalue cast as the appropriate type.
E.g.:
var item = (
string
) node.GetCustomFieldValue(fieldName);