Copied to clipboard

Flag this post as spam?

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


  • Jedd France 29 posts 140 karma points
    Jul 28, 2016 @ 16:11
    Jedd France
    0

    Get All Articles in Umbraco

    Hello everyone,

    I'm trying to write a MacroPartial that will allow me to pull out content from every item on my website that has a specific doctype.

    I'm building a blog for a friend and it will have different sections, but every article will have the same doctype. For the homepage I want to be able to select every article from every section, but I'm struggling to figure it out.

    Any ideas? Thanks guys!

    • Jedd
  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Jul 28, 2016 @ 16:19
    Alex Skrypnyk
    0

    Hi Jedd,

    You can do it with this code:

    var home = @CurrentPage.Site();
    var postNodes = home.AncestorOrSelf().Descendants("testDocTypeAlias");
    

    Thanks,

    Alex

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Jul 28, 2016 @ 16:22
    Nicholas Westby
    1

    You could try something like this (I don't have an IDE open, so this may include some mistakes, so consider it more as pseudo-code):

    var articles = Model.Content
        // This gets the node at the first level (i.e., the homepage node).
        .AncestorOrSelf(1)
        // This gets all descendants of the specified doctype.
        .Descendants("SomeArticleDocType");
    

    That will be slow, so you'll want to perform some caching (perhaps in a static variable).

  • Jedd France 29 posts 140 karma points
    Jul 28, 2016 @ 16:42
    Jedd France
    0

    Hi Alex,

    Thank you for your reply. I've implemented your code, and while I'm no longer getting an error message, it just doesn't return any content? Maybe I'm just being blind so I'll add a screenshot + my code

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    
    @{  var home = @CurrentPage.Site();
        var postNodes = home.AncestorOrSelf().Descendants("Article"); }
    
    @foreach (var item in postNodes)
    {
        <h3>@(item.GetPropertyValue("Title"))</h3>
    }
    

    enter image description here

    Any idea what I'm doing wrong?

    Thanks Alex,

    -Jedd

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 28, 2016 @ 16:45
    Dennis Aaen
    1

    Hi Jedd,

    You are using the dynamic Razor you you need to change

    @foreach (var item in postNodes)
    {
        <h3>@(item.GetPropertyValue("Title"))</h3>
    }
    

    To

    @foreach (var item in postNodes)
    {
        <h3>@item.title</h3>
    }
    

    Hope this helps,

    /Dennis

  • Jedd France 29 posts 140 karma points
    Jul 28, 2016 @ 16:55
    Jedd France
    0

    Hi Dennis,

    Thank you for your help. I've made the change you supplied but I'm still getting absolutely no content? Been staring at this for 45 minutes and can't see what the issue may be :(

    Thanks,

    Jedd

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Jul 28, 2016 @ 16:59
    Alex Skrypnyk
    0

    Hi Jedd,

    Can you debug your foreach?

    Or just try to write ids:

    @foreach (var item in postNodes)
    {
        <h3>@item.Id</h3>
    }
    

    Maybe 'postNodes' is empty?

    Thanks,

    Alex

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 28, 2016 @ 17:06
    Dennis Aaen
    0

    Hi Jedd,

    Could you please try this:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    
    @{  var home = CurrentPage.Site();
        var postNodes = home.Descendants("Article"); }
    
    @foreach (var item in postNodes)
    {
        <h3>@item.title</h3>
    }
    

    Hope this helps,

    /Dennis

  • Jedd France 29 posts 140 karma points
    Jul 28, 2016 @ 17:14
    Jedd France
    0

    Hi Dennis,

    Again I've implemented the code but still no output. I've looked over the code a thousand times and I still can't understand why it's pulling absolutely nothing from the article pages I've set up!

    Thanks,

    Jedd

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 28, 2016 @ 17:25
    Dennis Aaen
    1

    Hi Jedd,

    This code should give you the name of all your articles.

    @{  var home = CurrentPage.Site();
        var postNodes = home.Descendants("Article"); }
    
    @foreach (var item in postNodes)
    {
        <h3>@item.Name</h3>
    }
    

    If you donĀ“t get anything out, then please double check that your alias of your document type for the articles is Article.

    Normally when I debug code, then I am trying to do it in steps, so what you could try is to out put the home variable by do this:

        @{  
          var home = CurrentPage.Site(); 
        }
    
    @home.Name
    

    Did you then get the name of the home out? If so then you can add the next variable and see if you still get data out.

    Hope this helps,

    /Dennis

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Jul 28, 2016 @ 20:43
    Alex Skrypnyk
    1

    Hi Dennis and Jedd,

    Just test this code in Umbraco 7.4.3 and this is working:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        var home = Umbraco.TypedContentAtRoot().FirstOrDefault();
        var articles = home.Descendants("article");
    }
    
    @foreach (var item in articles)
    {
        <h3>@item.Id</h3>
    }
    

    Here no dynamic objects and please check alias of your article document type.

    Thanks,

    Alex

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Jul 29, 2016 @ 06:22
    Dennis Adolfi
    101

    Could it be something as simple as a casing issue? I noticed that in your code you wrote home.Descendants("Article") but Alex wrote home.Descendants("article") in the above example. Have your tried that? DocumentTypes usally starts with a small letter.

  • Jedd France 29 posts 140 karma points
    Jul 29, 2016 @ 09:31
    Jedd France
    0

    Hello everyone,

    Thank you all very much for your help, I think it may have just been a casing issue after all. I've managed to get the macro working now.

    Thanks again!

    Jedd

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Jul 29, 2016 @ 09:44
    Dennis Adolfi
    2

    Glad it worked out for you. This is exacly the kind of thing that one can go crazy from when one has been staring at a peace of code forever and turns out it something as simple as a casing issue. :)

    Have a great weekend! Take care!

Please Sign in or register to post replies

Write your reply to:

Draft