In this tutorial, you will learn how to retrieve a CMS page in code using Umbraco 8 and above. Umbraco 8 introduced a big change in the APIs. Gone are the days of UmbracoContext. API access is now done with constructor injection. This article will assume that you are not using the Model builder and that you need to get access to the current page.
The new API to get information about pages within Umbraco is the
IUmbracoContextFactory. Out-of-the-box Umbraco pre-configures the DI containers and registers all the Umbraco APIS. This means injecting the APIs is extremely easy. To get information about a page from Umbraco, you will be accessing the
IUmbracoContextFactory can be injected into a controller - or a custom class - like so:
After having access to 'IUmbracoContextFactory', you can get access to the page, like so:
If you do not have the name, you can also get access to pages by the content type id, like so:
Slightly bypassing good v8 practices, if you are within your code and you need access to the current page, you can do this:
You can get a list of pages by its document type, like this:
Using these techniques, you should be able to get good access to any page you need within Umbraco, happy coding!