Accessing a short-text custom page field with API from page-

Posted by Community Admin on 04-Aug-2018 02:12

Accessing a short-text custom page field with API from page-node

All Replies

Posted by Community Admin on 13-Oct-2015 00:00

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.????
    
 

Posted by Community Admin on 13-Oct-2015 00:00

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);

This thread is closed