How do you access the custom field values for the current page?
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.
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"
);
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.
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?
Hi Ed,
I have just replied in the following forum thread which you have opened related to this issue.
Regards,
Sabrie Nedzhip
Telerik