Search code examples
c#umbraco

Umbraco - DocumentType Field in c#


I'm trying to get an Umbraco Field in c# like this :

(String.Format(Umbraco.Field("labelFailure").ToString(), username));

But I'm getting the following error :

Cannot return the IPublishedContent because the UmbracoHelper was constructed with an UmbracoContext and the current request is not a front-end 
request.

I don't know the error and how to resolve it.

Thanks!


Solution

  • If you are not in a view you should use GetPropertyValue on the IPublishedContent of the page to get your value:

    ..
    using Umbraco.Web;
    ..
    
    var idPage = 1234; // you should get this dynamically :)
    IPublishedContent page = Umbraco.TypedContent(idPage);
    var labelFailure = page.GetPropertyValue<string>("labelFailure");