List Posts on a page belonging to a certain Category

Apr 20, 2015 at 1:57 AM
I want to list Posts belonging only to Projects category on my Projects custom page. I'm kinda new to C#. I see where you can list out Posts by Slug like this:
 <%=Post.Posts.Where(p => p.Slug == "project").FirstOrDefault().Title %>
but when I do the same thing, only with p.Category, it doesn't work. Do I need a listview control or an array? Help, please. Thanks!
Apr 20, 2015 at 1:20 PM
What version of BE are you using?
The most recent versions have a Post List widget, allowing you to select a specific category to display as a post list.
Assuming you are using a recent version that includes this widget then here is an example of a custom page(call it project.aspx in blog root) that contains nothing more than a widget zone.
When you navigate to this page and are logged in as admin, you can select add "Post List" from the Administration drop down. Once added you can configure number of posts and whether to show post thumbnail, description etc.
<%@ Page Language="C#" AutoEventWireup="true" Inherits="BlogEngine.Core.Web.Controls.BlogBasePage" Title="Project" %>
<%@ Import Namespace="BlogEngine.Core" %>

<asp:Content ID="Content1" ContentPlaceHolderID="cphBody" Runat="Server">        
    <blog:WidgetZone ID="WidgetZoneProjectList" runat="server" ZoneName="projectList" />
</asp:Content>
If you are using an earlier version of BE or the above is unsuitable then there are other options.
Marked as answer by khambley on 4/20/2015 at 7:41 AM
Apr 20, 2015 at 3:40 PM
I'm using the latest version, BE 3, so this should work, thank you!
Apr 20, 2015 at 3:58 PM
Edited Apr 20, 2015 at 4:03 PM
Ok, maybe I spoke too soon. I don't see "Post List" in the dropdown to add widgets and it is not in my widgets area in the admin console. How do I add that widget? Also, the checkbox next to the widget in the Admin console is grayed out, how do I select the widget? Screenshot - https://flic.kr/p/sbJpaG
Apr 20, 2015 at 4:28 PM
Not sure exactly when Post List was introduced, I think it may well have been on one of the v3 revisions, but if it's not showing then it must have been after your version.
I'm not sure if you downloaded the latest version and copied over the widget code that it would work, maybe.
However, here's an alternative. Note that c.Title=="exact category title match".
<%@ Page Language="C#" AutoEventWireup="true" Inherits="BlogEngine.Core.Web.Controls.BlogBasePage" Title="Project" %>
<%@ Import Namespace="BlogEngine.Core" %>
<%@ Register Src="Custom/Controls/PostList.ascx" TagName="PostList" TagPrefix="uc1" %>


<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        Category cat = Category.Categories.Find(delegate(Category c) { return c.Title == "Projects"; });
        if (cat == null)
        {
            PostList1.Posts = new List<IPublishable>(0);
        }
        else
        {
            PostList1.Posts = cat.Posts.ConvertAll(new Converter<Post, IPublishable>(delegate(Post p) { return p as IPublishable; }));            
        }
    }   
    
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="cphBody" Runat="Server">        
    <!--<blog:WidgetZone ID="WidgetZoneProjectList" runat="server" ZoneName="projectList" />-->
    <uc1:PostList ID="PostList1" runat="server" />
</asp:Content>
You will find code similar to this in default.aspx.cs, which is responsible for loading the various types of Post List i.e. by tag, by date, by cat etc.
So the above actually reloads the Post List control with what you require.