BlogEngine.NET Hierarchical Page Listing

I've been using the excellent .NET blogging platform BlogEngine.NET for a while now and really enjoy coding against it even though code highliting doesn't seem to work. The extensibility of the code is awesome and allows you to morph the program into whatever you like. I was recently converting the program into a CMS system for a project that required a simple management tool and realized that the Page listing was lacking a bit in its display. Granted, this tool was not meant to be a CMS system but if your blog has many pages this might be useful to you as well. The system has built in Parent -> Child paging but displays all of the pages straight out in a list. I wanted to see the list with the Parent pages in bold and the child pages indented beneith its parent. To accomplish this I added 2 properties to the Page.cs object in the Blogengin.core, then I added a function to the Pages.cs code behind in /Admin/Pages/Pages.aspx.cs and modified the BindPageList() function to call it.

Note: I recompiled the program as a .NET 3.5 project to make use of Lambda expressions in the BindChildPageList() function. Change it to use delegates in the for loop if you're using .NET 2.0

First I added two properties to the Page object to make things easier later on: [BlogEngine.Core/Page.cs]

        ///
        /// Does this post have a parent page?
        /// 
        public bool HasParentPage
        {
            get { return this.Parent != Guid.Empty; }
        }
        ///
        /// Does this post have child pages 
        /// 
        public bool HasChildPages
        {
            get
            {
                foreach (Page p in Page._Pages)
                {
                    if (p.Parent == this.Id)
                        return true;
                }
                return false;
            }
        }

Then I modified the BindPageList() function [BlogEngine.Web/Admin/Pages/Pages.aspx.cs]

private void BindPageList() {
        foreach (Page page in BlogEngine.Core.Page.Pages) {

            if (!page.HasParentPage)//MOD: Only add the page to the list if it's a root page (children are added below)
            {//MOD
                HtmlGenericControl li = new HtmlGenericControl("li");
                HtmlAnchor a = new HtmlAnchor();
                a.HRef = "?id=" + page.Id.ToString();
                a.InnerHtml = page.Title;

                System.Web.UI.LiteralControl text = new System.Web.UI.LiteralControl
                (" (" + page.DateCreated.ToString("yyyy-dd-MM HH:mm") + ")");

                li.Controls.Add(a);
                li.Controls.Add(text);

                if (page.HasChildPages)//MOD: If the page has children, add them recursively
                {//MOD
                    //find children
                    li.Controls.Add(BuildChildPageList(page));//MOD
                }//MOD

                ulPages.Controls.Add(li);
            }//MOD
        }

        divPages.Visible = true;
        aPages.InnerHtml = BlogEngine.Core.Page.Pages.Count + " " + Resources.labels.pages;
    }

And finally I added the BuildChildPageList() function [BlogEngine.Web/Admin/Pages/Pages.aspx.cs]

private HtmlGenericControl BuildChildPageList(BlogEngine.Core.Page page)
    {
        HtmlGenericControl ul = new HtmlGenericControl("ul");
        foreach (Page cPage in BlogEngine.Core.Page.Pages.FindAll(p => (p.Parent == page.Id)))
        {
            HtmlGenericControl cLi = new HtmlGenericControl("li");
            cLi.Attributes.CssStyle.Add("font-weight", "normal");
            HtmlAnchor cA = new HtmlAnchor();
            cA.HRef = "?id=" + cPage.Id.ToString();
            cA.InnerHtml = cPage.Title;

            System.Web.UI.LiteralControl cText = new System.Web.UI.LiteralControl
            (" (" + cPage.DateCreated.ToString("yyyy-dd-MM HH:mm") + ")");

            cLi.Controls.Add(cA);
            cLi.Controls.Add(cText);

            if (cPage.HasChildPages)
            {
                cLi.Attributes.CssStyle.Remove("font-weight");
                cLi.Attributes.CssStyle.Add("font-weight", "bold");
                cLi.Controls.Add(BuildChildPageList(cPage));
            }

            ul.Controls.Add(cLi);

        }
        return ul;
    }

The result is the following:


kick it on DotNetKicks.com


Related posts

Comments

April 11. 2008 02:00 PM

Troy Goode

Great idea. If this isn't already in for the next release, maybe you should submit it as a patch to the Codeplex project?

Troy Goode

April 11. 2008 03:23 PM

matt

@Troy

Thanks, and you're right, I will submit it to the Codeplex project for blogengine.net

matt

June 12. 2008 03:40 PM

Daniel

gracias por su valiosa información

Daniel

September 27. 2008 12:32 AM

article cycle

<a href=”http://articlecycle.com”>Articlecyle</a>
this is for the article cycle site

article cycle

October 10. 2008 02:56 PM

estudios grabacion

thanks for this theme

estudios grabacion

November 27. 2008 11:11 PM

Busby SEO Test

thanks for this codes and theme. ty

Busby SEO Test

December 3. 2008 12:11 PM

Busby SEO Test

Thank you for sharing this tips, i want to implementation now on my blogengine blog.

Busby SEO Test

January 4. 2009 10:05 AM

busby seo test

I'm quite new to BlogEngine.NET. but what you write in this post is really good. I think it will help me in the future. Thanks for the great work

busby seo test

January 17. 2009 02:10 AM

Busby SEO Test

thanks for the information you shared with us
this blog was so wonderful and interesting

Busby SEO Test

February 16. 2009 11:28 AM

Meeting rooms london

I am thinking to use blogengine.net, but I need to make sure the I can find some support forum in case something happen to blogengine. Any forum that I can refer to ?

Meeting rooms london

February 27. 2009 02:25 PM

Kampanya Damai Pemilu Indonesia 2009

So far no forum I think

Kampanya Damai Pemilu Indonesia 2009

March 3. 2009 05:40 AM

my little blogs

for me blogengine is the one i need ,promise

my little blogs

March 12. 2009 11:16 PM

Home of Natures

thanks for sharing your codes and it is really interesting.

Home of Natures

March 26. 2009 02:15 AM

designers clubs

keep up the good work, nice writings about your blog

designers clubs

April 23. 2009 04:03 AM

online lead generation

Great Work !

Regards
zimi

online lead generation

April 23. 2009 05:04 AM

tnomeralc web design toys

Very interesting programming site.. Very cool and useful.

tnomeralc web design toys

April 23. 2009 09:17 PM

Kampanye Damai Pemilu Indonesia 2009

the code makes me dizzy
oh, no...

Kampanye Damai Pemilu Indonesia 2009

May 8. 2009 06:07 AM

myspace graphic comment

from here i know something that i want to know..
thanks for this usefull informations..

myspace graphic comment

May 12. 2009 11:53 AM

Vinyl Banners

this is going useful to my site, thanks for the latest updates

Vinyl Banners

May 20. 2009 09:52 AM

Virtual Estudio

Thanks!!!
Gracias realmente me ha sido de utilidad

Virtual Estudio

May 21. 2009 07:03 AM

tukang nggame

just say amazing blog with the interesting and informative article

tukang nggame

May 27. 2009 03:44 AM

Mens Ties

Thanks for the great blog my friend

Mens Ties

May 30. 2009 08:52 PM

tukang nggame

Nice page

tukang nggame

June 10. 2009 12:18 PM

how to grow taller

That is a great post

how to grow taller

June 10. 2009 12:19 PM

how to grow taller

That is a great post

how to grow taller

June 10. 2009 12:20 PM

how to grow taller

That is a great post

how to grow taller

June 13. 2009 08:29 PM

Foto Tante Girang

Great Post, thanks..

Foto Tante Girang

June 13. 2009 08:30 PM

Koleksi Foto Bugil

Thanks for this information...

Koleksi Foto Bugil

June 17. 2009 06:06 AM

Butuan City

Hey, thanks for the codes and the themes!! Wonderful!

Butuan City

June 21. 2009 06:40 AM

Seleb

Heloo all, i love you all, thanks for this information..

Seleb

June 26. 2009 11:53 PM

SEO Tips

Thanks so much. I still find some ebook about Emulate in C# . Do you have it ?

SEO Tips

June 27. 2009 02:36 PM

the ridges in summerlin

Thanks for the tips. BlogEngine.NET is a good blogging engine. I use it and I've not had any problem with it but yes you're the coder so I can expect you to dig deep hehe.

the ridges in summerlin

June 30. 2009 12:38 AM

coconut grove real estate

BlogEngine.NET is the best engine I've ever used. I find the code very neat and simple.

coconut grove real estate

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

July 3. 2009 10:51 PM

Search

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009