In this tutorial, you will learn how to access the Umbraco home page in code. There are a few ways that you can get a reference to the homepage in code. To query for content within the CMS, you use the UmbracoHelper. This helper does not provide an out-of-the-box method like GetHomepage() to access the homepage. Instead you will need to do a simple query using UmbracoHelper to access it. How you query the CMS will depend on how you model the page hierarchy within the CMS. If you want to know which is the optimal technique to use on your site, read on 🔥🔥🔥

Getting The Homepage In A Simple Content Tree Structure

On most website builds, the site will be comprised of a single homepage and some child pages:

  • Homepage

    • Page One

    • Page Two

If your page tree looks like this, you can use this snippet to get the homepage from the CMS:

Accessing the UmbracoHelper in a controller or a view is really easy. If you want to access the UmbracoHelper in a custom class, you may need to instantiate the helper yourself. You can do that with the following snippet:

With a UmbracoHelper you can then use TypedContentAtRoot() to access the homepage 💥

Getting The Homepage Within A Multi-Node Situation

Some projects might have more than one page under the root node. For example, if you are working on a multiple language project (see here for more details), or, you created a setting page to store global data. In these situations, Umbraco.TypedContentAtRoot().First() will not cut the mustard, so what do you do?

Let us say you have this page-tree structure:

  • Homepage
    • Page One
    • Page Two
  • Mega Menu
  • Settings Page

Returning the first item using TypedContentAtRoot would not work in this set-up, as more than one page will be returned. There are two fixes to this dilemma:

  • Filter the results based on the document type alias
  • Hardcode the homepage Id in code and query using the Id. to get a page Id in Umbraco, open the homepage in the Umbraco backend and open the Properties tab. You should be able to see the Id:

How To Get The Home Page In Umbraco 1

After you know the Id, you can filter the results returned by TypedContentAtRoot like so:

I prefer option one, however, if option two makes you happy then there is nothing wrong with using it. If you don't know how

If you only have a single item at the top of your content tree, you can simply use Umbraco.TypedContentAtRoot().First() to access the homepage. If you have more than one page located at the root level, you will need to filter the results to get the homepage, sples 😊. Happy Coding 🤘