Using LINQ and EPiServer
Just as a brief proof of concept I decided to use a LINQ statement to retrieve and sort a list of EPiServer pages. If you haven't yet come across LINQ I would recommend Scott Guthrie's post on using LINQ.
LINQ can be used to query a number of different data sources such as SQL, XML or objects in memory. The latter is what I will do here. More specifically, I will use LINQ to query a PageDataCollection object.
Get the collection to query
The first step is a familiar one - I will simply retrieve all pages under a specified news container page:
const int newsContainerPageId = 4;
PageDataCollection newsPages =
DataFactory.Instance.GetChildren(
new PageReference(newsContainerPageId));
Query the page collection using LINQ
The following LINQ statement will query the page collection and only retrieve pages that are of type "[Public] News item". Furthermore, it will apply sorting to retrieve the pages in order of publishing date:
var pages = from p in newsPages
where p.PageTypeName == "[Public] News item"
orderby p.StartPublish //Sort by publishing date
select p; //Get all matching pages
Do something with it
Now, let's display our list in some way so that we can feel like we've accomplished something!
<asp:DataList ID="newsList" runat="server">
<ItemTemplate>
<a href='<%# Eval("LinkURL") %>'>
<%# Eval("PageName") %></a>
</ItemTemplate>
</asp:DataList>
Finally, let's databind our DataList control to our page list:
newsList.DataSource = pages;
newsList.DataBind();
Update: Just to clarify: before I was able to test LINQ with EPiServer I had to open up my EPiServer project in Visual Studio 2008, convert it and finally set the assembly properties to target version 3.5 of the .NET Framework.
10 March 2008