In this tutorial, you will learn about all of the essential APIs that you will need to use in order to build a kickass website powered by Umbraco CMS. When building any new Umbraco project, you will need to query the CMS for content. Umbraco provides a number of useful APIs out-of-the-box that will allow you to accomplish this. Knowing the correct API to use within the most appropriate situation is key to mastering this great open-source CMS. If you want to access some handy code snippets to make you a Umbraco BOSS, read on 🔥🔥🔥
How To Get The Current Page Id: When a site visitor tries to load a page on your website, the rendering pipeline will call Umbraco. Umbraco will try to map the requested URL to a page created within the CMS. If a successful match is made, Umbraco adds all the pages data into a property called the current page object. This object will contain all the data and content that a content editor has added within the CMS for that page, including the page Id. Depending on where you need to access the current page within your codebase, will determine how you access an API. In most situations, you will want to use route hijacking and access the object from within a controller. You can do this using this snippet:
You can also access the current page object within a view. Accessing code within a view is not ideal as it breaks the MVC paradigm. If you really want to, you can access the object using this snippet:
After accessing the current page Id, you will probably want to get a copy of the current page object. To get the current page object manually you can use the Document
object. To access the object you add the Id
as a constructor. Uou can get a populated object:
Umbraco also has a concept of a node. If you are building a backend plug-in or extension, you might want to access the Node
object rather than creating the object as a Document
. If you want to access the current page object as a Node
, you can use this snippet:
Umbraco also ships with some pre-built controllers that make life much easier when working with Umbraco objects. One of these controllers is called the SurfaceController
. You should always use a SurfaceController
when you want to create a custom form within Umbraco. The SurfaceController
will auto wire-up the routing rules that will allow the form to post back to the server. To access the current object within a SurfaceController
can be done like this:
How To Get The Current Pages Document Type in Umbraco: All pages within Umbraco are built from document-types. Sometimes you will need to manually check which document-type was used to create the current page. This can easily be done using this code:
Using the DocumentTypeAlias
property on the current page object will give you the data you need 💥
How To Get All Siblings Of The Current Page: Within Umbraco CMS, your website will be built from a hierarchy of pages. When building a menu, or a breadcrumb you will often need to query Umbraco to get data about the related pages adjacent to the current page within the CMS. A sibling relates to all the pages that contain the same ParentId
as the current page. If you want to get all the siblings that are related to the current page in a view, you can use this code:
How To Get All Children Of The Current Page: You can also get all the pages that have been created underneath the current page. This is done using this code:
Using Children()
will give you a list of IPublishedContent
items 😊
You now know about some very handy Umbraco API calls. Go out and build some cool shit. Happy Coding 🤘