How to get data about another page in Umbraco 7.7.1

Sometimes we need to return data from another page and display it on the page the user is currently on.

Depending on the amount of data we need to display will depend on how we achieve this.

In the code examples below, I will show you three ways that this can be done.

Option One

create a static class with a static IPublishedContent method, then on the page, we are on, we can pass in the page ID and retrieve the data we require.

I'm not a fan of this approach, as we are putting the code in the view which I'm not a great fan of.

Option Two

Again create a static class but this time have it only return the data we need.

Then we can call the static class and display the data we need in the view as shown below.

Option 3

My preferred option is to add a content picker to the settings doctype, this could be on the home page or a dedicated doctype that you use to add all your settings.

The advantage of this option is the content editor can delete the doctype you selected and add a new one to display the page. Then all they need to do is update the content picker with the new doctype and the CMS will do the rest.

Below is an example of how to do this.

Leave a comment