Search code examples
umbracoumbraco7

Umbraco Getting Value from Content


I've a little confusion over here, on this line of code

var cs = ApplicationContext.Current.Services.ContentService.GetById(1000);
cs.GetValue("test");

var nd = new Node(1000);
nd.GetProperty("test");

Both of that code can be used.. What is the different between that two code.. When and Why we use either one of them


Solution

  • Umbraco Services
    The service layer of the new umbraco API introduced in umbraco 6 includes a ContentService, a MediaService, a DataTypeService, and a LocalizationService. Check out the umbraco documentation for documentation on those services and the other umbraco services.

    The services in umbraco hit the database and don't leverage all of the caching that umbraco provides. You should use these services sparingly. If you are trying to programatically add/update/delete from the database or if you are trying to get unpublished content from the database, you should use these services. If all you need is to query for published content, you should use the UmbracoHelper because it is much faster.

    var cs = ApplicationContext.Current.Services.ContentService.GetById(1000);
    cs.GetValue("test");
    

    UmbracoHelper
    The UmbracoHelper is what you should almost always be using when you want to query content from umbraco. It doesn't hit the database and is much faster than the umbraco services.

    var node = Umbraco.TypedContent(1000);
    var nodeVal = node.GetPropertyValue<string>("test");
    

    If you find that you don't have access to the UmbracoHelper, you can make your own as long as you have an UmbracoContext:

    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    var node = Umbraco.TypedContent(1000);
    var nodeVal = node.GetPropertyValue<string>("test");
    

    NodeFactory
    The NodeFactory is obsolete. If you are using Umbraco 6 or higher, I would highly recommend converting to the UmbracoHelper.

    var nd = new Node(1000);
    nd.GetProperty("test");