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

Forums / Developing with Sitefinity / How do you access the custom field values for the current page?

How do you access the custom field values for the current page?

5 posts, 1 answered
  1. Ryan
    Ryan avatar
    5 posts
    Registered:
    07 Mar 2012
    17 Apr 2014
    Link to this post

    I've got Sitefinity 7.0 and created a custom field through the new interface. In this case, it's an image field called PageBannerImage. I've selected an image for a specific page and am now trying to pull and use this value from the code-behind of my master page.

    To get the current page I'm calling:

    var currentPage = SiteMapBase.GetActualCurrentNode();

    The Attributes collection under currentPage has the keys "RequireSsl" and "IncludeScriptManager", but not my custom attribute.

    I've tried:

    currentPage.GetCustomFieldValue("PageBannerImage")

    but this returns null.

    I've also tried getting the page via the fluent interface, but the Attributes for that are null.

    Answered
  2. Victor Velev
    Victor Velev avatar
    1345 posts
    Registered:
    06 Jul 2015
    21 Apr 2014
    Link to this post
    Hi Ryan,

    You can use the following code to get the value of a custom field of a page node:

    var man = PageManager.GetManager();
              var getCurrentPage = SiteMapBase.GetActualCurrentNode().Id;
              var page = man.GetPageNode(getCurrentPage);
              if (page != null)
              {
                  var customField = page.GetValue("Test");
              }

    In my case the custom field is called Test. Do not forget to reference Telerik.Sitefinity.Model as well.

    Regards,
    Victor Velev
    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
     
  3. Ryan
    Ryan avatar
    5 posts
    Registered:
    07 Mar 2012
    21 Apr 2014 in reply to Victor Velev
    Link to this post

    Victor,

    Thank you! I spent way too much time trying to figure this out. The documentation has examples on how to create and edit the field, but I couldn't find anything when it came to retrieving the value.

    For anyone else who is doing this, GetValue returns an object, which I cast to Telerik.Sitefinity.Libraries.Model.Image and used the MediaUrl property to get the url of the image.

  4. Ed
    Ed avatar
    20 posts
    Registered:
    26 Mar 2013
    08 Apr 2015 in reply to Ryan
    Link to this post

    I have a method where I obtain a list of pages: 

    public IQueryable<PageData> RetrieveCollectionOfPrograms()
            {
                PageManager pageManager = PageManager.GetManager();
                IQueryable<PageData> pages = pageManager.GetPageDataList();
                return pages;
            }

     I call that elsewhere, so that I may then print out a table containing one or more of the custom fields that I have for those pages. It sorta looks like this:

    var table = new List<CourseCatalogTableModel>();
    var pageCollection = RetrieveCollectionOfPrograms().AsEnumerable().OrderBy(x => Guid.NewGuid()).Take(1000);

                foreach(PageData page in pageCollection)
                {
                    table.Add(new CourseCatalogTableModel()
                    {
                        degreeProgram = collection.GetValue("Title"),
                        areaStudy = collection.GetValue("areaOfStudy");
                        
                    });
                } 

    The line where I am trying to access "areaOfStudy" does not work and I get an error saying "wrong custom field". Any idea how to access that?

  5. Sabrie Nedzhip
    Sabrie Nedzhip avatar
    534 posts
    Registered:
    09 Dec 2016
    13 Apr 2015
    Link to this post
    Hi Ed,

    I have just replied in the following forum thread which you have opened related to this issue.

    Regards,
    Sabrie Nedzhip
    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
     
5 posts, 1 answered