Within this tutorial, you will learn how to get a list of scheduled pages that are ready to be published using Umbraco CMS version 8. You may assume that getting a list of scheduled pages would be straightforward, however, Umbraco does not provide an out-of-the-box solution to do this. Meaning, you will have to write some custom code to make it so.

The first thing that needs consideration is how will you get the data? There are two tricky parts to this. First, pages that have not yet been published will not be returned from the frontend cache. This means we can not use the normal APIs to get this information from Umbraco. Normally you would work with IPublishedContent, however, this will not be possible in this use case. It would be possible to query the database directly, using ApplicationService, working with IContent items. The issue with this is performance. If you had to traverse every page within the content tree whenever the query is made, you will encounter performance issues. Instead, the best approach to do this will be to use a search index and Examine.

Out-of-the-box, Umbraco comes with two indexes that are used by Examine. The index called 'ExternalIndex' contains all the published items on your website. The second index is called 'InternalIndex' which contains all published and non-unpublished pages. As we need content related to unpublished pages you will need to query the 'InternalIndex'. It is also possible to create your own index. This is the approach I took to solve this problem!

To query an index you can use use the code below, passing in the 'indexName' to define which index you want to query, in this example 'InternalIndex':

This code is using the Examine search-indexing solution that comes shipped with Umbraco. Examine is the default Umbraco search provider that sits on top of a Lucene index. The files for these indexes can be found in the Appdata folder. The benefit of this approach is that the search will be quick and performant. The search will not make any database calls. The code above will only return pages that are not published. This happens on this line .Field("__Published", "n"). In this example, for reference '_Published' is an internal field within the index.

The thing to note about querying an Examine index is that it will not return a list of Umbraco pages directly. Instead, it returns search results. Within each search result, you will get the Umbraco ID for each item. You can then use that ID to query Umbraco. This querying can be done using the front-end cache. The front-end cache is another caching solution used by Umbraco to make things quicker. Do not get the search index and the frontend cache mixed up!

No Date

The next issues are dates! Published pages do not have a scheduled date. Umbraco has a scheduled data field, however, this is only available when the content is returned as IContent and not IPublishedContent. To query Umbraco to return an IContent you will need to use IContentService. An example of how to do this using constructor injection is shown below:

We now have everything to get a list of unpublished pages:

The code above first uses Examine to query the index and get a list of all the unpublished pages within the site. Next, we iterate through the search results collection, get the Umbraco ID from each search result, and use IContentService to query Umbraco to get data. When all the data in the list has been processing, order it by this date within descending order. You now have a complete list of all the pages that will be published.

If you are wondering where this code could live. You may consider creating a custom dashboard that lives within the CMS. Content editors can open the dashboard to see what content is scheduled for the upcoming months. Enjoy!