How to get Image field from page attributes - General Discussions - General Discussions - Progress Community
 General Discussions

How to get Image field from page attributes

  • How to get Image field from page attributes
  • Hello,
    I create  a RelatedMediaField to custum my "Edit Page" : (Administration->Advanced->ContentView->Controls->FrontEndPages->FrontEndPagesCreate)

    Problem : How can I get this image url ?
    node.GetValue<Telerik.Sitefinity.Libraries.Model.Image>("MyMediaField").Url;
    => Error : System.Exception: Wrong custom field 'MyMediaField' for type 'Telerik.Sitefinity.Pages.Model.PageNode'
    Maybe DataFieldName attribute bad done ?
    Please help.
    Thanks !!

  • Hello,

    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;

    Regards,
    Velizar Bishurov
    Telerik
     
    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