Copied to clipboard

Flag this post as spam?

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


  • kylepauljohnson 70 posts 108 karma points
    Jan 11, 2011 @ 21:45
    kylepauljohnson
    0

    Using the API to Retrieve Recent Blog Posts

    Hello dudes!.  I am looking for some direction/samples on how I might go about retrieving a list of blog posts using the API specifically rather than using XSLT.  I would be retrieving these from the Blog4Umbraco package.

    I have done a lot of hunting around the forums and played with some options however what I have found doesn't seem to meet the requirements, which are these two simple things:

    1. Only published content
    2. Recursively iterate though sub folders/sub content
    3. Order content by date published
    Anyone have any help?  Thanks so much!

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jan 11, 2011 @ 23:00
    Aaron Powell
    0

    You can do something like this:

    var blogRoot = new Node(1234);
    var latestBlogs = from post in bloggRoot.Children
                        orderby post.UpdateDate
                        select post;
    
    
    Or you can use LINQ to Umbraco if you don't know the starting node.
  • kylepauljohnson 70 posts 108 karma points
    Jan 11, 2011 @ 23:24
    kylepauljohnson
    0

    This acheives part of it, but it's not recursive so it simply returns me the years of the folders in the root level of the blog, in my case 2010, and 2011.  Below is the code I have currently.  Thanks for the help thus far.

     

    Dim blogRoot As Node = New Node(1133)
            Dim latestBlogs = From post
                In blogRoot.Children
            'Order By post.UpdateDatepost
    
            For Each i In latestBlogs
                Response.Write(i.Name)
            Next
  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jan 11, 2011 @ 23:28
    Aaron Powell
    0

    Ah right, if you're using autofolders then you're going to have a bit of a problem.

    Try this:

    var blogRoot = new Node(1234);
    var latestBlogs = from folder in bloggRoot.Children
                        from post in folder.Children
                        orderby post.UpdateDate
                        select post;
    
    
    A SelectMany against the folders should sort it out
  • kylepauljohnson 70 posts 108 karma points
    Jan 11, 2011 @ 23:47
    kylepauljohnson
    0

    Hopefully my last question... so when running the following vb code...

            Dim blogRoot As Node = New Node(1133)
    
            Dim latestBlogs = From folder In blogRoot.Children _
            From post In folder.Children _
            Order By post.UpdateDatepost

    I am getting an error telling me: "BC36593: Expression of type 'Object' is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the LINQ provider." on the following line:

    From post In folder.Children _

    Entirely possible I didn't translate the code properly.  Thanks again!

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jan 11, 2011 @ 23:52
    Aaron Powell
    0

    Well I don't know if the code compiles, I'm writing it in notepad :P

    Looking at the source Nodes (the type of Node.Children) is not a generic list, it's a non-generic list. You may need to do node.Children.Cast<Node>() so it becomes a generic list and you can query against it

    Add the .Cast<Node>() (or what ever the VB equivalent is) to each Children accessor

  • kylepauljohnson 70 posts 108 karma points
    Jan 12, 2011 @ 00:22
    kylepauljohnson
    0

    Got the issue sorted, and that gets me one level deeper to the months set of folders.  Code as follows...

            Dim blogRoot As Node = New Node(1133)
    
            Dim latestBlogs = From folder As Node In blogRoot.Children _
            From post In folder.Children() _
            Order By post.UpdateDate _
            Select post
    
            For Each i In latestBlogs
                Response.Write(i.Name & "<br>")
            Next
  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jan 12, 2011 @ 00:25
    Aaron Powell
    0

    Ok, then you'll just need to keep adding additional SelectMany statements to go from year folder to month folder to day folder (I forget how deep autofolders goes).

     

  • kylepauljohnson 70 posts 108 karma points
    Jan 12, 2011 @ 00:30
    kylepauljohnson
    1

    SWEET!  Got it.  Thanks so much!  Wish I could give you a "high five" but alas I have no karma.  :-)  Thanks man!

Please Sign in or register to post replies

Write your reply to:

Draft