Copied to clipboard

Flag this post as spam?

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


  • Owain Williams 481 posts 1413 karma points MVP 7x c-trib
    Jan 06, 2018 @ 22:03
    Owain Williams
    0

    Showing X number of posts via Controller

    Hi, I've created a controller which is used to display a title of the blog on the homepage. Just now I do this like so :

    List<BlogPostListModel> model = new List<BlogPostListModel>();
    IPublishedContent blog = CurrentPage.AncestorOrSelf(1).DescendantsOrSelf().Where(x => x.DocumentTypeAlias == "blog").FirstOrDefault();
    foreach (IPublishedContent blogpost in blog.Children)
    {
    model.Add(new BlogPostListModel(blogpost.Name));
    }
    return PartialView("~/path/to/partial", model);
    

    This is fine except, it displays all the blog titles, what I want to do is check to see if the number of blogs within the CMS is more than the number wanted from the backoffice. In the backoffice I have a slider which allows the user to pick how many blogs to show on the homepage. So if I have 10 blogs, I only want to show the 3 latest.

    I was thinking something like

    var numToShow = blog.GetPropertyValue<int>("howManyPostsShouldBeShown");
    if (blog.Children.Count() > numToShow)
    {
    
    }
    

    would work but my issue now is, how to I only show the number of blogs I want within the If statement?

    Hope that makes sense, been scratching my head all evening.

    O.

  • Nik 1612 posts 7258 karma points MVP 7x c-trib
    Jan 06, 2018 @ 22:17
    Nik
    100

    Hey Owain,

    You can use Linq to do this really quickly.

    var numToShow = blog.GetPropertyValue<int>("howManyPostsShouldBeShown");
    foreach (IPublishedContent blogpost in blog.Children.Take(numToShow))
    {
        model.Add(new BlogPostListModel(blogpost.Name));
    }
    

    (You might to do Children.Skip(0).Take(numberToShow) I'm not 100% on that though.)

    That will take upto the number you've selected to show.

    So, if blog.Children.Count() = 5 and numToShow = 6, it will only take 5. And if numToShow = 4 it will only take 4.

    Cheers,

    Nik

  • Owain Williams 481 posts 1413 karma points MVP 7x c-trib
    Jan 07, 2018 @ 13:48
    Owain Williams
    1

    Oh boy, I was close :)

            foreach (IPublishedContent blogPost in blog.Children)
                {
                    model.Add(new BlogPostListModel(blogPost.Name, blogPost.GetPropertyValue<string>("pageTitle"), blog.GetPropertyValue<int>("howManyPostsShouldBeShown")));
    
                }
    

    Just didn't have the .Take.

    Thanks everyone!!

  • Matthew Wise 271 posts 1373 karma points MVP 5x c-trib
    Jan 06, 2018 @ 23:55
    Matthew Wise
    1

    Yeah, take a look at Take in Linq like Nik said.

    Although I would say you may want to use examine / xpath for the query as Descendants calls are slower.

    Have a look at these great slides from dawoe (I can't remember his actual name :'( )

    https://www.slideshare.net/dawoe/umbraco-duugfest-17-the-need-for-speed

  • OleP 67 posts 276 karma points
    Jan 07, 2018 @ 10:26
    OleP
    0

    Thanks for the share. I rely heavily on the ContentService, so this might be a field where I can improve. But I'd say it depends on how many published items you've got. If OP only got a few houndred items, Children/Descendants is fine.

  • Matthew Wise 271 posts 1373 karma points MVP 5x c-trib
    Jan 07, 2018 @ 10:30
    Matthew Wise
    2

    The content service should not be used for front end requests, it makes database calls instead of calls to the Umbraco cache.

    The code above uses the cache as well.

Please Sign in or register to post replies

Write your reply to:

Draft