Copied to clipboard

Flag this post as spam?

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


  • Paul Seal 524 posts 2890 karma points MVP 7x c-trib
    Dec 08, 2016 @ 16:36
    Paul Seal
    1

    How can I do this in the controller?

    I want to get the home page from within the controller using the best method.

    This is how I am doing it from within the View:

    IPublishedContent homePage = Model.Content.AncestorOrSelf(1).DescendantsOrSelf().Where(x => x.DocumentTypeAlias == "home").FirstOrDefault();
    

    This is how I am doing it from within the Controller:

    int homePageId = int.Parse(CurrentPage.Path.Split(',')[1]);
    IPublishedContent homePage = Umbraco.Content(homePageId);
    

    How can I use the first approach from within the controller?

  • Jonathan Richards 288 posts 1742 karma points MVP
    Dec 08, 2016 @ 16:46
    Jonathan Richards
    100

    Hi Paul,

    If what is missing is the full list of Extension methods like AncestorOrSelf() and DescendantsOrSelf() on IPublishContent, that you wish to use in your controller code, you need to add

    using Umbraco.Web;
    

    to the top of your controller code.

    Cheers

  • Paul Seal 524 posts 2890 karma points MVP 7x c-trib
    Dec 08, 2016 @ 17:44
    Paul Seal
    0

    This is exactly what I was looking for. Thanks Jonathan.

    Paul

  • Alex Skrypnyk 6182 posts 24283 karma points MVP 8x admin c-trib
    Dec 08, 2016 @ 16:49
    Alex Skrypnyk
    0

    Hi Paul

    What controller are you using?

    You can do it with this code:

    Umbraco.AssignedContentItem.AncestorOrSelf(1)
    

    Thanks,

    Alex

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies