+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Accessing a short-text custom page field with API from page-node

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

2 posts, 0 answered
  1. MB
    MB avatar
    302 posts
    Registered:
    09 Jan 2005
    13 Oct 2015
    Link to this post

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

  2. MB
    MB avatar
    302 posts
    Registered:
    09 Jan 2005
    13 Oct 2015 in reply to MB
    Link to this post

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

2 posts, 0 answered