In this tutorial, you will learn how to query the Umbraco CMS editor in order to get data about the current page. On every Umbraco 7 build, you will undoubtedly need to get information about the current page in order to render some data added by content editors onto a page. Umbraco provides multiple mechanisms to do this In this guide. Depending on where you need to access the current page will determine which approach you will need to use. In this guide, you will learn about all the different options so you will always know how to access the data you need. If you want to learn how to do this, read on 🔥🔥🔥
Before we look at the code, let us think about performance. There are two main ways of querying content from Umbraco, one is via a cache. Querying for content via the frontend cache is quick. 99% of the time you should work with the APIs that work with the cache. This will ensure your pages load quickly 💨💨💨
Umbraco also provides APIs to query content from the CMS database directly. As you would expect, the APIs that query the data consumes more resources and are slower. Use these APIs frequently and your website performance will be impacted. In general, you only work with these APIs if you want to save content back to the CMS in code. A simple tip is that if you work with an API and it returns an item of type IPublishedContent
it has gone through the cache. If you use an API and it returns an item of type IContent
the request has gone via a database query.
it is important to use the correct API for the task at hand. Next, we will look at which API you should use and in what circumstances:
Getting The Current Page Via The Cache: To get data from the cache you need to use the UmbracoHelper
. You can do this using this code:
Getting access to the helper in a view or a Umbraco controller is easy. You can access the UmbracoHelper
via a Umbraco supplied HTML helper or an exposed property. If you are in a vanilla MVC controller, you might have to manually create the UmbracoHelper
yourself. You can manually new up a helper using this code:
Getting The Current Page Via The Database: To query content via the database you need to use a different API. You will need to use ApplicationContext
. The code to access a page using this API is shown below:
The Legacy Way (Umbraco 6 and below): In Umbraco V6 and below, you can also access the current page using Node.GetCurrent()
. Within V7, Node.GetCurrent()
is considered a legacy API and it is not recommended that you use it. Node
queries data directly from the database, instead of the XML cache. The code to access the page object is shown below:
If you try to implement Document
within your code, you will see a deprecated error. If you stumble across this code online, you now know that you should avoid using it 🤔
Getting The Current Page In A View: If you want to access the current page in a view then this is also not a problem. In general, if you want to access the current page in a view, I would recommend thinking about your architecture. If you follow the MVC paradigm, the view should simply render data passed into it by a view model and not calculate it directly. In terms of architecture, it is better to add code within a controller rather than a view, however, do what makes you happy 😊
If your view inherits from UmbracoTemplatePage
you can access the current page using this code, @CurrentPage
.
You can also use a type-safe helper using this snippet @Model.Content
.
If you need get the current page ID
in a view, you can use either of the following techniques:
That covers all the different ways of querying for content in Umbrco. You should now know which API to use and when. Happy Coding 🤘