Accessing value for custom fields applied to pages
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?
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
Can you please share what the solution was here? I am having the same issue. Thank you.
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"
);