The challenge – The site that we will be coding will have its pages tagged with episerver categories. Implement a control that will list all the pages tagged with a specific category.
The control aspx code seems looks pretty straightforward and is derivative of some other controls that are defined in the EPiServer sample site:

<%@ Control Language=”C#” AutoEventWireup=”false” CodeFile=”CategoryListing.ascx.cs” Inherits=”development.templates.Units.CategoryListing” TargetSchema=”http://schemas.microsoft.com/intellisense/ie5″ %>

<%@ Register TagPrefix=”EPiServer” Namespace=”EPiServer.WebControls” Assembly=”EPiServer” %>

 

<div id=”rightmenudivStartPage”>                       

    <div class=”listheadingcontainer”>

        <div class=”listheadingleftcorner”></div>

        <a class=”listheading leftfloating” href=”<%=EventRootPage.LinkURL%>“><%= EventRootPage.PageName %></a>

        <div class=”listheadingrightcorner”></div>

    </div>

 

    <EPiServer:PageList runat=”server” ID=”PageList1″>

        <ItemTemplate>

            <div class=”startpagecalendaritem”>

                <span class=”datelistingtext”>

                <episerver:property ID=”Property1″ runat=”server” PropertyName=”PageLink” CssClass=”StartCalendar” />

                <span class=”Normal”><episerver:property ID=”Property3″ runat=”server” PropertyName=”MainIntro” /></span>

            </div>

        </ItemTemplate>

    </EPiServer:PageList>

    <br/><br/>

</div>

The wirst thing you will notice after looking at the code is that PageList is pretty much a standard ASP.NET reinvented and rehashed. GREAT! Sounds like we can use the Data binding, right? That’s also true:

private void Page_Load(object sender, System.EventArgs e)

{

    if (!IsPostBack)

    {

        PageList1.DataSource = CategoryPages;

        DataBind();

    }

}

Does the trick of binding the data to the repeater… uh… oh… sorry… I mean PageList.

Now comes the hard and non-obvious part.

How does one actually find the pages assigned to a category? Short of calling the database directly to query the tblCategorypage table that comes to mind at first, it looks like EPiServer has a really cool page finding module that allows for simple yet fairly effective discovery of pages conforming to some developer defined criteria. Those criteria are defined by means of forming any necessary number of instances of PropertyCriteria class provided as a PropertyCriteriaCollection to Global.EPDataFactory. The result is handed to us back as a PageDataCollection. The fetching of the pages looks like this:

public PageDataCollection GetCategoryPages()

{

    PropertyCriteria criteria = null;

 

    CategoryList categories = ((PropertyCategory)(CurrentPage.Property[“AggregatedCategory”])).Category;

 

    criteria = new PropertyCriteria();

    criteria.Condition = CompareCondition.Equal;

    criteria.Type = PropertyDataType.Category;

    criteria.Value = categories.ToString();

    criteria.Name = “PageCategory”;

    criteria.Required = true;

 

    PropertyCriteriaCollection col = new PropertyCriteriaCollection();

    col.Add(criteria);

 

    PageDataCollection pdc = new PageDataCollection();

    pdc = Global.EPDataFactory.FindPagesWithCriteria(EPiServer.Global.EPConfig.StartPage, col);

 

    return pdc;

}

For this to work though, one has to define some more stuff in the web admin part of the site. the usual task is to define the Page Template for the control. In this case when you define the Page Template make sure to put the “AggregatedCategory” property in that should be of type Category selection. This property defines what categories you want your control to aggregate, and you can provide one or more of those. the other property that is required for this very control is “CategoryContainer” of type Page. This is solely for the purpose of having a master page that you define as the “main page” for the chosen category selection. It serves as a clickable header in the control. Unnecessary maybe but one would have to define a caption for the control anyway so why not make it meaningful.

The usage of the said control is trivial. A sample usage on the default EPiServer site looks like this:

<%@ Page language=”c#” Codebehind=”CategorySummary.aspx.cs” AutoEventWireup=”false” Inherits=”development.Templates.CategorySummary” %>

<%@ Register TagPrefix=”EPiServer” Namespace=”EPiServer.WebControls” Assembly=”EPiServer” %>

<%@ Register TagPrefix=”development” TagName=”CategoryListing”  Src=”~/templates/Units/CategoryListing.ascx”%>

<%@ Register TagPrefix=”development” TagName=”DefaultFramework”    Src=”~/templates/Frameworks/DefaultFramework.ascx”%>

 

<development:DefaultFramework ID=”DefaultFramework” runat=”server”>

    <EPiServer:Content ID=”Content1″ Region=”fullRegion” runat=”server”>

        <development:CategoryListing  ID=”CategoryContent” runat=”server”/>

    </EPiServer:Content>

</development:DefaultFramework>

Out of which really only the <development:CategoryListing  ID=”CategoryContent” runat=”server”/> part is meaningful.

For the record… It looks like THE place to go to solve that kind of dilemas fast is the EpiServer “Developer to Developer” forums.

*)… couldn’t ressist with the title joke :) For those not in subject… It’s a reference to a silly commercial of one of the market chain (Biedronka) in Poland that advertises recently with “My first *something*… from Biedronka*

Cognifide
The article is based on the knowledge I’ve gathered and work I’ve performed for Cognifide. Cognifide is an official partner EPiServer and the real contributor of the the control.

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...



This entry (Permalink) was posted on Tuesday, December 19th, 2006 at 7:20 pm and is filed under ASP.NET, C#, EPiServer, Internet Information Services. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response , or trackback from your own site.



  • tdi

    great title Adam.. the rest is not my field by great code colours :P

  • Do you realize the performance penalty for doing such a recursive search through the entire episerver tree? This sort of control is what brings intranets / websites to it’s knees 1 year later and noone understands why everything got so slow all of a sudden. Even on a small webb with 5-10 thousand pages that will have a very negative impact on performance.

    If you ever have to search through an entire tree like that I highly suggest you use caching to keep some performance. With this control you could easily cache for a very long time and hook the SavedPage event in global.ascx to invalidate the cache when actually needed.

  • Hey Merri,

    Thanks for the constructive comment.

    I didn’t really do any performance testing of this control since it was only for the purpose of learning and intended more as a quick technology demo than anything else.

    Agreed, caching on a wide search like this is always a wise choice.

  • Livin da loca