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 131 karma points c-trib
    Dec 03, 2013 @ 13:24
    Thomas
    1

    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...

    http://our.umbraco.org/Documentation/Reference/Mvc/querying

    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 69 karma points
    Dec 04, 2013 @ 20:43
    Erik
    2

    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:

    Umbraco.TypedContent(Node.getCurrentNodeId());
    
  • Thomas 35 posts 131 karma points c-trib
    Dec 05, 2013 @ 10:51
    Thomas
    105

    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: http://our.umbraco.org/forum/developers/api-questions/37894-new-Dynamic(id)-and-new-Node(id)-equiv-in-v6?p=5

  • Dirk Seefeld 122 posts 632 karma points
    Jul 17, 2015 @ 13:51
    Dirk Seefeld
    2

    In a helper method you can also use this:

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

    see: https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/

    ;-)

Please Sign in or register to post replies

Write your reply to:

Draft