Try Now
More in this section

Forums / General Discussions / How to get Image field from page attributes

How to get Image field from page attributes

2 posts, 0 answered
  1. Medy
    Medy avatar
    15 posts
    05 Oct 2015
    27 Nov 2015
    Link to this post

    I create  a RelatedMediaField to custum my "Edit Page" : (Administration->Advanced->ContentView->Controls->FrontEndPages->FrontEndPagesCreate)

    Problem : How can I get this image url ?
    => Error : System.Exception: Wrong custom field 'MyMediaField' for type 'Telerik.Sitefinity.Pages.Model.PageNode'
    Maybe DataFieldName attribute bad done ?
    Please help.
    Thanks !!

  2. Velizar Bishurov
    Velizar Bishurov avatar
    143 posts
    25 Apr 2017
    02 Dec 2015
    Link to this post

    Make sure you filter the page node by the current frontend root node id to ensure you are getting the correct root node. Also if you have not explicitly specified that only 1 image can be selected you have to specify the generic parameter as "List<Telerik.Sitefinity.Libraries.Model.Image>".

    Try the following code:

    var pageManager = PageManager.GetManager();
    var pageNode = pageManager.GetPageNodes().Where(pn => pn.Title == "Schools" && pn.RootNodeId == SiteInitializer.CurrentFrontendRootNodeId).FirstOrDefault();
    var images = pageNode.GetValue<List<IDataItem>>("MyMediaField").Cast<Telerik.Sitefinity.Libraries.Model.Image>();
    var mediaUrl = images.FirstOrDefault().MediaUrl;

    Velizar Bishurov
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
2 posts, 0 answered