Accessing value for custom fields applied to pages

Posted by Community Admin on 04-Aug-2018 14:54

Accessing value for custom fields applied to pages

All Replies

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

I added a custom field to my pages called "areaOfStudy". When trying to access the "Title" field like this, it works:

title = page.GetValue<Lstring>("Title");

But I get this error:

System.Exception: Wrong custom field 'areaOfStudy' for type 'Telerik.Sitefinity.Pages.Model.PageData' when I try to access 'areaOfStudy' like this:

areaOfStudy = page.GetValue<Lstring>("areaOfStudy");

Any ideas? It's odd that it seems to know I am trying to access a custom field, and that it doesn't work when title does, which is not a custom field. Thoughts?

 

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

Hello Ed,

You should be able to get properly the value of the field using the sample code you have provided.

Judging by the error message saying Wrong custom field 'areaOfStudy' for type 'Telerik.Sitefinity.Pages.Model.PageData'  it seems that Sitefinity is not able to find a custom field with the name areaOfStudy for pages. Can you please make sure that the name of the custom field which you use in the code sample is correct?

In addition to this, please check the type of the field and if it is not a short text or a long text field, please make sure that you are properly specifying the type of the field in the GetValue() method.

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
 

Posted by Community Admin on 02-Nov-2015 00:00

Can you please share what the solution was here?  I am  having the same issue.  Thank you.

Posted by Community Admin on 05-Nov-2015 00:00

Hi Monika,

Can you please try to get the custom field from the page node and not from the page data and you should not have problems to get the value of the custom field. Here is a sample for your convenience:

var pageManager = PageManager.GetManager();
var pageNode = pageManager.GetPageNodes().Where(p => p.Title == "PageTitle").FirstOrDefault();
  
var customFieldValue = pageNode.GetValue("FieldNameValue");


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