Copied to clipboard

Flag this post as spam?

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


  • Rik Hodiamont 56 posts 156 karma points
    Feb 20, 2015 @ 09:55
    Rik Hodiamont
    0

    Get current node api umbraco 7

    Hi,

    I use the new ContentService API for a while now. But I was wondering how I could load the current page ID.

    I use this 'var childerencontent = contentService.GetDescendants(100);' to get all the Descendants below a node, but the problem is I want to make this page very flexibel, so the usercontrol can be used in several nodes and I don't want to create a usercontrol for every node.

    Any ideas?

    Tnx in advance.

    Rik

  • Sören Deger 733 posts 2844 karma points c-trib
    Feb 20, 2015 @ 10:04
    Sören Deger
    1

    Hi Rik,

    with this you can get the current nodeId:

    var nodeId =Convert.ToInt32(HttpContext.Current.Request.QueryString["id"])

    Then use

    var current = contentService.GetById(nodeId);

    to get your current node.

    Documentation of ContentService: https://our.umbraco.org/documentation/Reference/Management-v6/Services/ContentService

    Some times is better to use IPublishedContent, because contentservice hit the database and IPublishedContent hit the cached umbraco.config file (see here: https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/).

     

    Hope this helps,

     

    Best, Sören 

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Feb 20, 2015 @ 10:20
    Dave Woestenborghs
    0

    Hi Rik,

    First of all if you are using the ContentService API for displaying content you are doing it wrong. This API hits the database directly for querying content and can become a performance bottleneck if your site has a lot of traffic. Have a look at the IPublishedContent object for querying content for displaying. See the documentation here : https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/

    If you want to manipulate data (edit the content) than you should use the contentservice api. The documentation for that can be found here : https://our.umbraco.org/documentation/Reference/Management-v6/Services/ContentService

    Dave

     

  • Rik Hodiamont 56 posts 156 karma points
    Feb 20, 2015 @ 10:45
    Rik Hodiamont
    0

    Hi Sören and Dave,

    Tnx for your quick response!

    @Sören, sorry but the Request.QueryString isn't working.

    I would like to use the Umbracohelper but in the examples I also see a prefilled ID. Do you have an example of the UmbracoHelper getting an node ID?

    Thanks in advance.

    Rik

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Feb 20, 2015 @ 10:47
    Dave Woestenborghs
    0

    With the umbraco helper you can do this.

    @Umbraco.AssignedContentItem : this will return the current item.

    Dave

  • Rik Hodiamont 56 posts 156 karma points
    Feb 20, 2015 @ 10:51
    Rik Hodiamont
    0

    Hi Dave,

    Is this also working in an usercontrol?

    I referenced the umbraco.dll in my VS project.

    I use the following code:

    int nodeID = Umbraco.Web.UmbracoHelper.AssignedContentItem;

    and I got this error:

    Error 14 An object reference is required for the non-static field, method, or property 'Umbraco.Web.UmbracoHelper.AssignedContentItem.get'

    Thank you for pointing me in the right direction.

    Rik

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Feb 20, 2015 @ 11:13
    Dave Woestenborghs
    0

    I think you need this code :

     var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);

    var currentItem = umbracoHelper.AssignedContentItem; 

    Dave

  • Rik Hodiamont 56 posts 156 karma points
    Feb 20, 2015 @ 13:54
    Rik Hodiamont
    100

    Hi Dave,

    I got an error on that code. VS didn't recognize the UmbracoContext.Current.

    But I used this:

    int nodeID = Convert.ToInt32(Umbraco.Web.UmbracoContext.Current.PageId);

    And it works :)

    Thanks for your help!

    Rik

Please Sign in or register to post replies

Write your reply to:

Draft