Copied to clipboard

Flag this post as spam?

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


  • kodaxmax 5 posts 45 karma points
    Nov 02, 2023 @ 00:04
    kodaxmax
    0

    Getting and displaying the 10 or x most recently updated pageson umbraco 12

    I can get every published page recursively and display the ones of the documentTypes i want.

    But im having trouble converting these pages (IPublishedContent into some sort of list or collection to be sorted by date.

    I tried just appending the "content" var in the loop to an array, list, collection and ienumerable of IPublishedContent. But the list was always empty at runtime. The code below works, but im not sure how i would make it display ordered by lastUpdateDate.

    string[] docTypes = new string[] { "modPage"};
    var results = Umbraco.AssignedContentItem.Children().Where(x => x.IsVisible());
    void DisplayAllPagesOfType(IEnumerable<IPublishedContent> pages)
    {
        @foreach (var content in pages)
        {
            if (content != null)
            {
                bool isDoctypeMatch = false;
                foreach (string docTyp in docTypes)
                {
                    if (content.IsDocumentType(docTyp))
                    {
                        isDoctypeMatch = true;
                        break;
                    }
                }
    
                @if (isDoctypeMatch)
                {
                    <div class="childBlock">
                        <a href="@content.Url()" class="indexBlockBtn btn" type="button"><b>@content.Name</b></a>
                        @if (content.Value<IEnumerable<IPublishedContent>>("gallery") != null)
                        {
                            var images = content.Value<IEnumerable<IPublishedContent>>("gallery");
                            if (images.Count() > 0)
                            {
                                <img class="img-fluid" src="@images.FirstOrDefault().GetCropUrl()" alt="@images.FirstOrDefault().Name" />
                            }
                        }
                        <div style="text-align:center">
                            @Html.Raw( content.Value("blurb"))
                            @Html.Raw(content.Value("fullDescription"))
                        </div>
                    </div>
                }
    
            }
            var children = content.Children().Where(x => x.IsVisible());
            DisplayAllPagesOfType(children);
        }
    }
    
    @{ DisplayAllPagesOfType(results); }

    heres an updated script attempting to display by uiterating over a list of iPublishedContents. It executes without error, but only display a "0" in H1 for each returned page

    string[] docTypes = new string[] { "modPage"};
    var results = Umbraco.AssignedContentItem.Children().Where(x => x.IsVisible());
    
    List<IPublishedContent> GetAllPagesOfType(IEnumerable < IPublishedContent > pages)
    {
        List<IPublishedContent> resultsList = new List<IPublishedContent>();
        @foreach (IPublishedContent content in pages)
        {
            if (content != null)
            {
                foreach (string docTyp in docTypes)
                {
                    if (content.IsDocumentType(docTyp))
                    {
                        resultsList.Append(content);
                        <h1>@resultsList.Count()</h1>
                        break;
                    }
                }
            }
            var children = content.Children().Where(x => x.IsVisible());
            GetAllPagesOfType(children);
        }
    
        return resultsList;
    }
    
    void DisplayPageBlock(IPublishedContent content)
    {
    
        var images = content.Value<IEnumerable<IPublishedContent>>("gallery");
        <div class="childBlock">
            <a href="@content.Url()" class="indexBlockBtn btn" type="button"><b>@content.Name</b></a>
            @if (images.Count()>0)
            {
                    <img class="img-fluid" src="@images.FirstOrDefault().GetCropUrl()" alt="@images.FirstOrDefault().Name" />
            }
            <div style="text-align:center">
                @Html.Raw( content.Value("blurb"))
                @Html.Raw(content.Value("fullDescription"))
    
              </div>
        </div>
    }
    void DisplayAllPageBlock()
    {
        foreach (IPublishedContent content in GetAllPagesOfType(results))
        {
            DisplayPageBlock(content);
        }
    
    }
    

    }

    @{ DisplayAllPageBlock(); }
  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Nov 03, 2023 @ 12:56
    Marc Goodson
    0

    Hi Kodaxmax

    I'm not completely sure what you are trying to achieve but, if I've understood correctly, you want an IEnumerable of IPublishedContent containing Children of the current page that are of a certain Document Type?

    If so then the following:

    string[] docTypes = new string[] {"modPage"};
    IEnumerable<IPublishedContent> acceptableChildren = Umbraco.AssignedContentItem.Children().Where(x=>x.IsVisible() && docTypes.Contains(x.ContentType.Alias)).OrderByDescending(x=>x.UpdateDate);
    

    But if there is a specific DateTime property you have added to the Document Types that you want to order by, eg a custom Publish Date you've added then it would be:

    string[] docTypes = new string[] {"modPage"};
    IEnumerable<IPublishedContent> acceptableChildren = Umbraco.AssignedContentItem.Children().Where(x=>x.IsVisible() && docTypes.Contains(x.ContentType.Alias)).OrderByDescending(x=>x.Value<DateTime>("publishDateAlias"));
    

    But let me know if it is something more complex than that and I will try to elaborate further!

    regards

    Marc

  • kodaxmax 5 posts 45 karma points
    Nov 04, 2023 @ 04:42
    kodaxmax
    0

    I can use that to simplify some of my code, but the problem is that, that only fetches direct children. Not children of children and so on.

    I have a simple site that hosts mods for video games. i have a custom document type "modPage". Every mod has it's own page. However they are not in the same directory. Each game is a directory or idnex page with many modPages as children.

    So even if i run that code for eevery single directory i would end up with an ordered list from every directory, rather than one ordered masterlist of all modPages from every directory.

    My posted code iterates through every single published page and displays every modpage. But has no way to order them. As each valid page was iterated i was trying to add it to a list or collection and then sort that masterlist by updateDate (which is default umbraco property, though i might be spelling it wrong here). But the list always returned empty for some reason.

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Nov 04, 2023 @ 16:45
    Marc Goodson
    100

    Hi Kodaxmax

    How about using Descendants instead of Children?

    string[] docTypes = new string[] {"modPage"};
    IEnumerable<IPublishedContent> acceptableChildren = Umbraco.AssignedContentItem.Descendants().Where(x=>x.IsVisible() && docTypes.Contains(x.ContentType.Alias)).OrderByDescending(x=>x.UpdateDate);
    

    it's essentially what you are trying to do?

    regards

    Marc

  • kodaxmax 5 posts 45 karma points
    Nov 04, 2023 @ 23:50
    kodaxmax
    0

    it's essentially what you are trying to do?

    I belioeve it is, many thanks!

Please Sign in or register to post replies

Write your reply to:

Draft