I’ve decided that it would be a good idea to make a short post on something simple, to give you the break from the epic series and posts I’ve been posting lately. So, in this post I’ll explain how to create a simple user control that displays x number of last modified pages. Take a look at the Figure 1 to see what I have in mind (if you don’t care and just want to get the code, you can do it here).
Figure 1 – Site Activity control displays last pages that have been modified with their titles, link to them and time at which they have been modified
The markup I’ve made is rather simple – repeater and unordered list. Here is how the front end of this control looks like:
|<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SiteActivity.ascx.cs" Inherits="MyControls_SiteActivity" %> |
|<asp:Repeater ID="siteActivitityRepeater" runat="server"> |
| <HeaderTemplate> |
| <ul> |
| </HeaderTemplate> |
| <ItemTemplate> |
| <li> |
| <asp:HyperLink ID="pageLink" runat="server"></asp:HyperLink> |
| <br /> |
| <asp:Label ID="pageDate" runat="server"></asp:Label> |
| </li> |
| </ItemTemplate> |
| <FooterTemplate> |
| </ul> |
| </FooterTemplate> |
As I’ve said – nothing special here. We’ll bind the repeater to the data source and on the ItemDataBound bind the pageLink HyperLink control and pageData Label with respective data.
I’ve commented most of the code and its logic is very simple – so I won’t go into much detail here. You’ll see that we are using CmsManager which is the API class responsible for any pages/templates/controls work. This is the method I’ve used to get the list of pages:
|CmsManager manager = new CmsManager(); |
|int totalRows = 0; |
|IList pages = manager.GetPages(0, activityCount, "DateModified", ListSortDirection.Descending, out totalRows); |
The first two arguments are paging arguments : start and count. I’ve specified the control to start at 0 and get the number of modified pages that user specified through ActivityCount property. Then we have the sort expression, which in our case is “DateModified” and when we pass descending sort direction after that – we get exactly what we want – last x number of modified pages. The last output parameter is not used in my code, however API mandates it so I’ve just declared it there.
Quick installation guide
To get this control working please do following:
- Download the project from here
- Unzip the project and paste the MyControls folder in the root of your website
- Register the control in Sitefinity toolbox by placing following line as a child of telerik/cms/toolboxControls (after the clear):
|<add name="Site Activity" section="Most popular" url="~/MyControls/SiteActivity.ascx" /> |
And that’s it.