List all scheduled pages in Episerver

At our company, we have a web site with a few pages that goes in and out of published state. For example, we are running a recurring campaign for a product that is sold periodically. At the end of every campaign the page is expired. When the campaign starts next time the page is activated again. The campaign allways starts at midnight and thus has to be scheduled (We have normal working hours). The scheduling is accomplished simply by setting the published property in the settings tab to a date in the future (you will get a warning saying “Expiration date cannot be prior to the published date” but the warning will vanish when expiration is removed). This is perhaps not the most “correct” way of scheduling a page but the Webmaster wants a list of all scheduled pages, no matter of how the scheduling is accomplished. That is particullary important since we have tousands of subscribers that receive an email when certain pages become published. The code is running in Episerver version 9.12.


public class ScheduledPageInfo
{
  public PageReference PageRef { get; set; }
  public DateTime PublishDate { get; set; }
}

public class Util 
{
  public IEnumerable<ScheduledPageInfo> getDelayedPublished(PageData rootPage)
  {
  var Repo = ServiceLocator.Current.GetInstance<IContentRepository>();
  var VersionRepo = ServiceLocator.Current.GetInstance<IContentVersionRepository>();
  var RefList = Repo.GetDescendents(rootPage.PageLink);
  List<ScheduledPageInfo> PageList = new List<ScheduledPageInfo>();

  foreach (PageReference PageRef in RefList)
  {
    PageData Page = Repo.Get<PageData>(PageRef);

    //Covers the cases: 
    //1. StartPublish is set to a future date on an already published page (that is not yet 
    //   visible to the visitors).
    //2. A new page, i.e. no prior versions, that is "Scheduled for Publish".
    //
    //Note! It is necessary to check the Status of the page, Page.StartPublish is something
    //like now plus one second for a page with Status set to VersionStatus.CheckedOut.
    if (Page.StartPublish >= DateTime.Now &&
      (Page.Status == VersionStatus.Published || Page.Status == VersionStatus.DelayedPublish))
      {
        PageList.Add(new ScheduledPageInfo
      {
        PageRef = PageRef,
        PublishDate = Page.StartPublish
      });
    }

    //Covers the case when a page that is "Scheduled for Publish" has prior versions. 
    //IContentRepository.Get(PageReference) returns the currently published version 
    //when there are more versions for the given reference, and that is not what we want.
    //In order to find future versions, we loop through all versions of a page, using 
    //the default implementation of IContentVersionRepository.
    else if (Page.Status == VersionStatus.Published)
    {
      var Versions = VersionRepo.List(PageRef);
      foreach (var Version in Versions)
      {
        if (Version.Status == VersionStatus.DelayedPublish)
        {
          PageList.Add(new ScheduledPageInfo
          {
            PageRef = PageRef,
            PublishDate = (DateTime) Version.DelayPublishUntil
          });
          break;
        }
      }//foreach
    }
  }//foreach
  return PageList.OrderBy(p => p.PublishDate);
  }//getDelayedPublished
}//class