Using LINQ and EPiServer

by: Ted Nyberg (Hallvarsson & Halvarsson)

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();
 
image


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


Comments

  1. Have anyone tested this for performance issues? I would like to use LINQ instead of FindPagesWithCriteria, but I'm not sure what's most efficient...
  2. Lars, since you are querying an in-memory collection you would have to initially retrieve all pages that should be a part of your search. If you have a large site this might not be the best thing to do. Otherwise, it would be a good alternative to the "FindPagesWithCriteria" method.
Post a comment    
User verification Image for user verification  
Ted Nyberg (Hallvarsson & Halvarsson)

About me

I work for the corporate communications consultancy Hallvarsson & Halvarsson in Stockholm, Sweden.

My main focus areas are architecture, design and implementation of online applications such as public web sites and intranets.

Live Messenger

Hallvarsson & Halvarsson logo
 MCPD, MCTS and MCP logos


Syndications


Archive


Tag cloud

EPiTrace logger