Copied to clipboard

Flag this post as spam?

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


  • dave 36 posts 149 karma points
    Apr 29, 2015 @ 17:21
    dave
    0

    Articulate Blog Post on home page

    I would like to put a full blog post on my home page by category. i.e. i want to get the last blog post with a category of 'test' for example.

    I was able to display recent blog posts and I figured i could just modify slightly.

    var root = Model.RootBlogNode; //Model.AncestorOrSelf(1);
    var blogRoot = root.Children;
    var blogArchive = root.Children.First();
    var entries = blogArchive.Children.Where(x => x.Properties.Where(a=>a.PropertyTypeAlias=="categories" && a.HasValue=="Test"));
    

    I was hoping entries above would give me all blog entries which have a category of 'Test'. I do have one entry which has a category of 'Test' I can step through code and I see the property categories.

    I get the following error CS1593: Delegate 'System.Func

    I am thinking this may just be linq issue.

    thx

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Apr 29, 2015 @ 18:10
    Alex Skrypnyk
    0

    Hi Dave,

    Can you rewrite this line:

    var entries = blogArchive.Children.Where(x => x.Properties.Where(a=>a.PropertyTypeAlias=="categories" && a.HasValue=="Test"));
    

    Maybe like that :

    var entries = blogArchive.Children.Where(x => x.GetPropertyValue<string>("categories") == "Test"));
    

    Thanks, Alex

  • dave 36 posts 149 karma points
    Apr 29, 2015 @ 18:51
    dave
    0

    I tried but no blog entries returned. No exception which is good but not the solution. categories is a property inside a collection of each entry which i feel is the big clue.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Apr 29, 2015 @ 19:45
    Dennis Aaen
    0

    Hi Dave, 

    If I understand your questions correct you have a home page node, in your Umbraco installation, and at the same level you have the Articulate blog node. And then you want to display blogposts from a category on your home page. See the image below.

    With this code you should get blogpost where the category is Test.

    @{
        var blogRoot = Umbraco.ContentAtRoot().DescendantsOrSelf("Articulate").FirstOrDefault();
        var blogArchive = blogRoot.Children.First();

        foreach(var blogposts in blogArchive.Children.Where("Visible").Where("categories == @0","TestCategory")){
            <p>@blogposts.Name</p>
        }

       
    }

    The first thing I do is go the root of the Umbraco tree, and from there you need to find the blog node, and then we find the first children in our case it´st he blog archive. Then I am looping though each blogpost where the category is set to Test.

    You can use to make a partial view or a partial view macro file, the code you should use is the same.

    For the documentation about the .ContentAtRoot take a look here: https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/#ContentAtRoot%28%29

    And here is also a strongly typed vesion available: https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/#TypedContentAtRoot%28%29

    Hope this helps and don't hesitate to ask again,

    /Dennis

  • Shannon Deminick 1524 posts 5269 karma points MVP 2x
    Apr 29, 2015 @ 23:11
    Shannon Deminick
    0

    Something like this?

    var root = Model.RootBlogNode;
    //first child of type ArticulateArchive
    var blogArchive = root.Children.OfTypes("ArticulateArchive").First();
    //To lookup the 'last' item (assuming you mean 'latest' item)
    var entries = blogArchive.Children
        .OrderByDescending(x => x.UpdateDate)
        //Get the latest item that has been tagged with a category of "Test" (case insensitive)
        // categories are stored as CSV so we split
        .First(x => x.GetPropertyValue<string>("categories").Split(',').InvariantContains("Test"));
    
  • dave 36 posts 149 karma points
    Apr 30, 2015 @ 03:03
    dave
    0

    I appreciate the help.

    Dennis (you can see below the exception/error) using blogArchive.Children.Where("Visible").Where("categories == @0","TestCategory")) You can see a few other variations that i tried without success

    enter image description here

    Shannon I did get some success with (shown here) which is good. How would I get all blog entries that have a category of Test (or .net features). I noticed the First in the syntax but wasn't sure how i could change out to get all of the blog entries.

    var entries = blogArchive.Children.First(x=>x.GetPropertyValue<string>("categories").Split(',').InvariantContains(".NET Features")); //works
    

    thanks for the support.

  • Shannon Deminick 1524 posts 5269 karma points MVP 2x
    Apr 30, 2015 @ 06:23
    Shannon Deminick
    100

    try something like this:

    var root = Model.RootBlogNode;
    //first child of type ArticulateArchive
    var blogArchive = root.Children.OfTypes("ArticulateArchive").First();
    //To lookup the 'last' item (assuming you mean 'latest' item)
    var entries = blogArchive.Children
        //This ensures the latest is first
        .OrderByDescending(x => x.UpdateDate)
        //Get all latest items that has been tagged with a category of "Test" (case insensitive)
        // categories are stored as CSV so we split
        .Where(x => x.GetPropertyValue<string>("categories").Split(',').InvariantContains("Test"))
        .ToArray();
    
  • dave 36 posts 149 karma points
    Apr 30, 2015 @ 13:14
    dave
    0

    I got object reference not set to an instance of an object on the .Where line

    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    Source Error: 
    Line 30:         //Get all latest items that has been tagged with a category of "Test" (case insensitive)
    Line 31:         // categories are stored as CSV so we split
    Line 32:     .Where(x => x.GetPropertyValue<string>("categories").Split(',').InvariantContains("Test"))
    Line 33:     .ToArray();
    Line 34:     
    

    However I was able to modify the where clause slighted so that I avoid those posts that have null value via the following which seemed to work

    .Where(x => x.GetPropertyValue<string>("categories") !=null && x.GetPropertyValue<string>("categories").Split(',').InvariantContains("Test"))
    

    The ToArray was a missing piece. Thanks again!

  • Shannon Deminick 1524 posts 5269 karma points MVP 2x
    May 01, 2015 @ 01:37
    Shannon Deminick
    0

    Yes, it's because you need a null check there, if the property is empty, it will be null

Please Sign in or register to post replies

Write your reply to:

Draft