One common requirement on every Umbraco build is getting a reference to the homepage from your code. In this post I'm going to cover the ways you can acheive that with Umbraco. 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 heirachey, read on to learn more.
Gettting 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:
Content -- 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 an Umbraco controller, then you may need to instantiate your own Helper. You can do that with the following snippet:
var umbracoContext = Umbraco.Web.UmbracoContext.Current; var umbracoHelper = new Umbraco.Web.UmbracoHelper(umbracoContext); umbracoHelper.TypedContentAtRoot();
Gettting 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. Some examples of when you might want to create mulitple pages under the root node, include mult-language webite 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.
Content -- 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:
var rootNodes = Umbraco.TypedContentAtRoot(); var homeNodeById = rootNodes.First(x => x.Id == 1077);
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:
var rootNodes = Umbraco.TypedContentAtRoot(); rootNodes.FirstOrDefault(x => x.DocumentTypeAlias == "umbHomePage");
In today's guide, I've covered everything you should hopefully need to get a reference to the homepage from the Umbraco 7 API. 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.