Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Thomas 35 posts 133 karma points c-trib
    Dec 03, 2013 @ 13:24

    Get current node as IPublishedContent

    Hi all,

    Apologies if this has been asked before but I'm struggling to find any documentation for what I'm looking for. I have already looked at this section...

    In Umbraco 4.7+ you could do something like this to get access to the current node using DynamicNode...

    var Node = Node.GetCurrent();
    var DynamicNode = new DynamicNode(Node.Id);

    In Umbraco 6, with MVC this was replaced with IPublishedContent. So, the question is how would you do something like the above to get the current node as an IPublishedContent object?

    Note, I am not talking about Razor views and want to do this in a helper class of some sort.

    Thanks Tom

  • Erik 14 posts 70 karma points
    Dec 04, 2013 @ 20:43

    You can use this:

    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    IPublishedContent content = umbracoHelper.TypedContent(Node.getCurrentNodeId());

    Alternatively, if you're on a controller that inherits from SurfaceController, you can just do this:

  • Thomas 35 posts 133 karma points c-trib
    Dec 05, 2013 @ 10:51

    Thanks Erik, I like the UmbracoHelper object. Looks very useful!

    In searching around though, we can do better...

    On a controller that inherits from SurfaceController, you actually have access to the CurrentPage object that is exposed in the views so don't need to call Node.getCurrentNodeId()

    And outside of that scope (in 6.1+) you can use...

    var currentNode = UmbracoContext.Current.PublishedContentRequest.PublishedContent;

    See this post for a more detailed discussion:

  • Dirk Seefeld 123 posts 661 karma points
    Jul 17, 2015 @ 13:51
    Dirk Seefeld

    In a helper method you can also use this:

    UmbracoHelper umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    IPublishedContent currentPage = umbracoHelper.AssignedContentItem;



Please Sign in or register to post replies

Write your reply to: