One common requirement within every Umbraco website is to get a reference to the homepage within code. In this tutorial, you will learn how to do that. There are several ways that you can get a reference to your homepage in code.  The technique you use will depend on how your content editors/you are planning on setting up your page-hierarchy, read on to learn more.

Getting The Homepage In A Simple Content Tree Structure

On most website builds, you may only have one node as the root node, like in the following situation:

  • Homepage
    • Page One
    • Page Two

In these scenarios, you can use this snippet to get the homepage from the Umbraco Helper:

One thing to note is that if you're trying to use the Umbraco Helper in a custom class etc.. outside of a Umbraco controller, then you may need to instantiate your own Helper. You can do that with the following snippet:

Getting The Homepage In A More Content Tree Structure

If you have a slightly more complex page tree structure, as seen below, where you have multiple pages under your Umbraco root node, calling the code above will result in more than one page being returned and consequently mean that when you call the TypedContentAtRoot you don't get the homepage.  

Situations where you might want to create multiple-pages under the root node include. multi-language website sites, or when you have a complex mega-menu and you need a menu builder that requires it's own area in the content tree (see here for more details), or even a setting page to store global data.

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

In these situations, you will need to filter out the results. One way to do this is via the document type alias, using a hardcoded string is a bit risky in case a content editor ever updated the name. A safer approach is to use the homepage's content id. If you don't know how to get a page Id in Umbraco, then open a page in the Umbraco backend and go to the 'Properties' tab for the home page in the content section:


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

If like me you're not very keen on hardcoding ID in your code, a more sage approach would be to filter by document type alias, because in theory you should never have more than one homepage. You can do this like:

 If you only have a single item at the top of your content tree, then you can simply use Umbraco.TypedContentAtRoot().First(). If you have more than one page located at the root level, then you will need to filter the results to specifically get the homepage rather than a list of pages under Umbraco root.