How to use blog in other areas of my site

Topics: ASP.NET 2.0, Business Logic Layer
Jun 19, 2009 at 5:13 AM

Hoping this isn't a stupid question... but,

I'd like to have my last 5 blog posts titles (linking to full blog post) on my home page. Is there a way for me to query the posts XML and display the top 5 posts? Is that possible?

If so how is that done?

Thank you for your help!

Coordinator
Jun 19, 2009 at 7:51 AM

If your home page and BE are running in the same application, then the posts are already going to be in memory.  Here's a quick sample on how to access the posts.  You'll want to include a "using" statement for BlogEngine.Core in the top of your code-behind page:

using BlogEngine.Core;

... the code:

foreach (Post p in Post.Posts.GetRange(0, 5))
{
    System.Web.UI.HtmlControls.HtmlAnchor a = new System.Web.UI.HtmlControls.HtmlAnchor();
    a.InnerText = p.Title;
    a.HRef = p.AbsoluteLink.ToString();
    Page.Controls.Add(a);
    Page.Controls.Add(new LiteralControl("<br />"));
}

Jun 19, 2009 at 4:18 PM

You're the man Ben, Thank you so much.

Where can I find the documentation for BlogEngine.Net... actually I found it doesn't explain much... is there like a msdn for blogengine.net that explains each method etc? Or how did you learn it?

What I'd also like to do is rank or essentially keep certain posts on the homepage regardless of publication date.

If I could set a ranking value and have it pull posts by rank first and by pub date last that'd the best or

Something like:

if (Posts Title = "xyz") {

linkX = xyz etc.

}

Appreciate your time if it's too much work for you no worries, I really appreciate your help. you've definitely got me on the right track!

JL

Coordinator
Jun 20, 2009 at 9:48 AM

I went to BlogEngine.NET school, and learned this stuff there.  Ok, just kidding :-)  There's some documentation here, but for me, just looking at the code for the functionality I need has been a good way to learn the ins-and-outs of BE.

Here's some more examples on things you can do.  The first example finds a post by Title.  The second example find's a category by name, and if the category is found, all the posts marked with that category are retrievable through someCategory.Posts.  Something like this could be used to get all the posts for a certain category if you marked the posts you want to retrieve later with a certain category.

Post somePost = Post.Posts.Find(
    delegate(Post post) { return post.Title.Equals("your post title", StringComparison.OrdinalIgnoreCase); });

Category someCategory = Category.Categories.Find(
    delegate(Category cat) { return cat.Title.Equals("some category", StringComparison.OrdinalIgnoreCase); });

if (someCategory != null)
{
    List<Post> categoryPosts = someCategory.Posts;
}

Jul 2, 2009 at 1:03 AM

Hey Ben,

I'm trying to implement the following code:

foreach (Post p in Post.Posts.GetRange(0, 1))
{
    System.Web.UI.HtmlControls.HtmlAnchor a = new System.Web.UI.HtmlControls.HtmlAnchor();
    a.InnerText = p.Title;
    a.HRef = p.AbsoluteLink.ToString();
    Page.Controls.Add(a);
    Page.Controls.Add(new LiteralControl("<br />"));
}

However I keep getting an error at "foreach (Post p in Post.Posts.GetRange(0, 1))"

The error reads:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Do I need to instantiate Posts or the BlogEngine somehow? I do have two posts in there right now.


Appreciate your help