A Umbraco website, like any website, is made up of a collection of pages. In Umbraco, each different type of page is defined by creating a corresponding document type within Umbraco. If you want to query the CMS to render data from a specific page you will need to use the
ID property. Whenever you create a page in the CMS it will be given an
ID can be found either by looking at the URL when you view the page in the editor. It can also be found within the
properties tab. Using this page
ID, you can query Umbraco in order to get the pages associated document type and all its data.
To display the data associated with a document type, you will need to use one of the Umrbacoi APIs. You have two main options, the content service or the Umbraco helper. One way queries the database directly. This should only be used if you need to save a page in page. The other faster way uses a cache. This is the approach you should normally use. If you want to learn about the differences between
IPublishedContent, I recommend reading, How To Reduce The Number Of SQL Calls Within Your Umbraco pages.
Getting Data From
Assuming you want to only read data from the CMS use the
UmbracoHelper. The code to do this is shown below:
This code gets the data about a page from the Umbraco cache. This is the quickest way to get and display data on your web pages. To query the API you pass the Umbraco Id to the
TypedContent() method. This will return your Umbraco page in the form of an
IPublishedContent object. After you have a reference to a page of
IPublishedContent object, you can get values out of it in two ways. First, you can use the
GetPropertyValue() method, like so:
If you don't know the underlying property type that you want to query
GetPropertyValue() is probably your best bet.
GetPropertyValue() will simply return the property as an object if it exists. You can also type the type returned from
GetPropertyValue() like this:
This second approach is better as it will result in more robust code!
Getting Data From IContent
It is also possible to use the content service to get data from Umbraco. If you want to access page data so you can update something and save it back to the database in code, use this API. You can tell when you are working with an API that queries the database as it will be of type
To get a reference to a Umbraco page, you pass the
Id to the
GetById() method. This will return your Umbraco page in the form of an
IContent object. After you have a reference to an
IContent object, you can get values out of it in two ways. First, you can pass the property name into the
GetValue() method, like so:
If you don't know the property type that you want to work with,
GetValue() is your best bet.
GetValue() will simply return the property as an object, assuming it exists! If you know the property type you can also explicitly type the call like this:
In the snippet above, instead of
GetValue() returns an object, it will return a string instead. This code is more efficient as it will prevent you from needing to call
ToString() on it later on. As I mentioned, the benefit of using
IContent is that you can do CRUD operations. Just so you can see an update in action, in the snippet below I'm using the
SetValue() method to update the page.
This code alone won't update the Umbraco database. If you want to save the data back to Umbraco, you will need to use the
SaveAndPublishWithStatus() method, like so:
I am not really a big fan of having to use hardcoded strings to access properties. Hardcoding a property name in code is a recipe for future bugs and it's a code smell in my opinion. Unfortunately, out-of-the-box Umbraco doesn't support document-type class modelling. From my understanding, some form of model binding will be available from Umbraco 7.5... so there is hope!
In the meantime, if you want to use C# class to represent document types, you can look into packages like uSiteBuilder, or UmbracoMapper.
I think ultimately, that Zbu.ModelsBuilder will be the package that gets integrated into Umbraco, so you may want to use that. I would assume the upgrade path later will be a bit easier later on. Personally, I am currently using uSiteBuilder, however, it definitely does have its limitations.
That's pretty much all the basics of getting page data out of Umbraco CMS. The recommended way is to work with
IPublishedContent, however, if you need to update or modify page data you will need to use
IContent. To architect your website in a nice way, I would suggest that you look into model binding your Umbraco page into a C# class. Which package you use is up to you! Happy Coding 🤘