How do you access the custom field values for the current pa

Posted by Community Admin on 04-Aug-2018 16:23

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

All Replies

Posted by Community Admin on 17-Apr-2014 00:00

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.

Posted by Community Admin on 21-Apr-2014 00:00

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
 

Posted by Community Admin on 21-Apr-2014 00:00

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.

Posted by Community Admin on 08-Apr-2015 00:00

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?

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

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
 

This thread is closed