How To Get A Property From An Umbraco Content Page

An 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.  To display a page created by a content editor within Umbraco on your website will involve you getting the current page ID and passing that back to Umbraco in order to get the associated document type and all the data associated with it.  To display the data associated with a document type, you will need to use either the content service of the Umbraco helper to get the content from front-end side.  If you are unsure about the different types of content you can us within Umbraco (IContent or IPublishedContent), I recommend reading, How To Reduce The number Of SQL Calls Within Your Umbraco pages.  

Getting Data From IPublishedContent

@using Umbraco.Web;

var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); var umbracoPageId = 1; IPublishedContent content = umbracoHelper.TypedContent(umbracoPageId); content.GetPropertyValue<>()

 As this method gets the data about a page from the 'cache', this is the quickest way to get and display data on your web pages.  To get a reference to an Umbraco page, 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 an IPublishedContent object, you can get values out of it in two ways.  First, by passing the properties names into the GetPropertyValue() method, like so:


If you don't know the type GetPropertyValue() is your best bet, as this will simply return the property as an object if it exists.  If you know the property type you can also type it:


Getting Data From IContent

 You can use the content service API within your website, by using the following call for IContent (the IContent API is slow as it uses the database, on the plus side it allows you to update and save content)

            var umbracoPageId = 1;
            var iContent = ApplicationContext.Current.Services.ContentService.GetById(umbracoPageId);

To get a reference to an Umbraco page, you pass the Umbraco 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, by passing the properties into the GetValue() method, like so:


If you don't know the type GetValue() is your best bet, as this will simply return the property as an object, if it exists. If you know the property type you can also type:


In the snippet above, instead of GetValue returning an object, it will return a string instead, preventing you from needing to call it's ToString() method.  As I mentioned, the benefit of using IContent is that you can do CRUD operations, just so you can see in the snippet below, I'm using the Setvalue() method to update the page.  

iContent.SetValue("propertyName", "data");

This 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:


Strongly-Typed Models

As we use .NET I'm not really a big fan of having to use hardcoded string to access my Umbraco properties. Unfortunately, out of the box Umbraco doesn't support this type of feature, however, from my understanding some form of model binding will be avilable from Umbraco 7.5... so there is hope!  In the meantime, if you want to bypass calling properties yourself, you can look into packages like uSiteBuilder, or using the UmbracoMapper.  I think ultimately, that Zbu.ModelsBuilder will be the thing that gets intergrated into Umbraco, so you may want to use that. I would assume the upgrade path later will be a bit easier :)  Personally I use uSiteBuilder, but it does have it's limitations at times.


That's pretty much all the basics of getting page data out of an Umbracopage.  The quickest way is to work with IPublishedContent, but it you need to update or modify that 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.  As of writing, Umbraco doesn't support this out of the box, so you will have to use a third-party package.  Zbu.ModelBuilder looks like it will be intergrated within Umbraco soon, so that's probably your best bet, however, I personally tend to favour uSiteBuilder.  Enjoy!

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top