Show Published Pages (Versions) in a Specific Time Span

blog header image

Since I'm still trying to learn my way around EPiServer I grab whatever chance I get of coding something a bit out of the ordinary, and today was no different. At a Developer Course I was attending, the need came up for a page that would list all the versions of all the pages on a given EPiServer CMS 5 web site, published within 2 specific dates - and just for the fun and the exercise I coded this little sample.

The Code-Behind for this page is pretty simple: It recursively goes through the site and builds a list of PageVersion objects (from the EPiServer.DataAbstraction namespace) whenever it comes across a version of a page that is published in the time period given as input (in this sample it's hardcoded - but that could easily be changed).

 

    public partial class VersionList : EPiServer.TemplatePage
    {

        private List<PageVersion> ValidPages = new List<PageVersion>();

        private void FindVersions(PageReference startpage, DateTime start, DateTime stop)
        {
            //Get all the versions of the page we are currently examining
            PageVersionCollection pvc = EPiServer.DataAbstraction.PageVersion.List(startpage);
            foreach (PageVersion pv in pvc)
            {
                if ((pv.Status == VersionStatus.PreviouslyPublished)||(pv.Status==VersionStatus.Published))
                {
                    //We now have a version that has been published
                    DateTime pub = pv.Saved;
                    //It's safe to assume the Saved property holds the publish-time, since any later saves would result in newer versions
                    if ((pub >= start) && (pub <= stop))
                    {
                        ValidPages.Add(pv);
                    }
                }
            }
            //Recursive through childpages
            foreach (PageData pd in GetChildren(startpage))
            {
                FindVersions(pd.PageLink, start, stop);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //Loop through all pages, find pages published within a time period
            FindVersions(PageReference.StartPage, new DateTime(2007, 1, 1), new DateTime(2007, 12, 1));

            //And databind to a Repeater control
            PageList1.DataSource = ValidPages;
            PageList1.DataBind();
        }
    }

 

 

You may wonder: "Why is he doing this recursively when he just as easily could have retrieved a list of all pages and done it iteratively?".

- "Well, it's elementary my dear Watson. Recursive programming is always more fun."

In the ASPX I just put a couple of lines to output the versions:

 

<%@ Page Language="c#" Inherits="KnowledgeWeb.VersionList" Codebehind="VersionList.aspx.cs"
    MasterPageFile="~/Templates/MasterPages/KnowledgeMaster.Master" %>

<%@ Register Assembly="EPiServer.Web.WebControls, Version=5.1.422.4, Culture=neutral, PublicKeyToken=8fe83dea738b45b7"
    Namespace="EPiServer.Web.WebControls" TagPrefix="EPiServer" %>
<asp:Content runat="server" ContentPlaceHolderID="LeftAndMiddleSection">
    <asp:Repeater ID="PageList1" runat="server">
        <ItemTemplate>
            <p>
                <%# (Container.DataItem as EPiServer.DataAbstraction.PageVersion).Saved.ToShortDateString() %>
                ,
                <%# (Container.DataItem as EPiServer.DataAbstraction.PageVersion).Name %>
                , Language:
                <%# (Container.DataItem as EPiServer.DataAbstraction.PageVersion).LanguageBranch %>
                , ID:
                <%# (Container.DataItem as EPiServer.DataAbstraction.PageVersion).ID %>
            </p>
        </ItemTemplate>
    </asp:Repeater>
</asp:Content>
Recent posts