Home Way to access individual fields of pages to display on a different page - Umbraco
Reply: 1

Way to access individual fields of pages to display on a different page - Umbraco

Witted
1#
Witted Published in 2018-02-12 16:32:42Z

I have a question about how to access fields from pre-existing pages and display them in a different page.

For example I have a document type called: "People" and I create a page for several people so that the structure of my content section looks like this:

Home
    page1
    page2
    page3

People
    person1
    person2
    person3

The document type "People" uses contains the fields: Name, Age, Job, Description all as textboxes.

What would you suggest is the best way of accessing the values in these fields for each page so that you loop through each person under the parent "People" and display their name/age/desc?

Using:

  @{
        var selection = Umbraco.TypedContent(1108).Children()
                            .Where(x => x.IsVisible());
    }
    @foreach(var item in selection){

    }

I can only access the metadata for each page such as @item.Id but I cant work out how to access the fields so say @item.Name returns the persons name.

Any help will be really appreciated! Cheers.

Claus
2#
Claus Reply to 2018-02-12 23:01:55Z

What you get from the query above is instances of your content as IPublishedContent. This is a pretty generic interface for any type of published content, so you will not be able to directly access the specific custom properties you have defined on your document types, as normal C# properties on this object.

However - if you have ModelsBuilder enabled in your site, you should be able to do the following and get the children returned as mapped POCO classes:

var selection = Umbraco.TypedContent(1108).Children<Person>()

(or <People> .. depending on what the actual document type alias of the child items is called)

If you are not using ModelsBuilder, you would have the option of just doing this inside your foreach loop to get the values of your properties instead:

var age = item.GetPropertyValue<string>("age");

Bonus note: please rename your Name property to something else (fullname or something like that). Using Name will clash with the built-in property used for the node name :)

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.39256 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO