Within the 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 tasks is straight-forward, however, Umbraco does not provide an easy-to-use API so you will have to write some code.
The first thing that needs consideration is how will you get the data. There are two tricky parts to this. First, as the data is not yet published it will not be returned within
IUmbracoContextFactory. If you have to query every page using an API to traverse the page tree you will likely encounter performance issues.
With that in mind, the nest approach to take to solve the problem is to query an index to get a list of unpublished pages. Then instead of working with
PublishedContent query the database directly using
IContent to get the scheduled date.
First, let's start off by querying an index. Out-of-the-box, Umbraco comes with two indexes. An index called 'ExternalIndex' that contains published items. The second index is called 'InternalIndex' that contains published and unpublished pages.
As we need content related to unpublished pages you will need to query the 'InternalIndex'. It may be useful to note that it is also possible to create your own index. This is the approach I took. To query the index you can use this method, passing 'InternalIndex' as the 'indexName'
This code will use to Examine. Examine is the default Umbraco search provider that sits on top of a Lucene index. Querying examine will perform a search against an index stored within the Appdata folder. The benefit of this approach is that the search will be quick. The search will also not make any database calls either. 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 the examine index is that it will not return a list of pages. Instead, it returns search results. Within a search result, you will get the Umbraco ID. You can then use that ID to query Umbraco and get information about the scheduled date.
If you use Umbraco a lot you may be thinking that whenever you have not seen a scheduled date. This is because the scheduled data property is only available when Umbraco returns it as a
IContent. The reason why you probably haven't used
IContent much is that performance-wise it is not very good. Each query will make a database call bypassing any cache. Be mindful of this limitation as you may need to add caching around this code.
To query Umbraco to return an
IContent you will need to use
IContentService. Within Umbraco you can do this using dependency injection, specifically within a constructor, like this:
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. This list should hopefully be semi-small. 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 about that page.
Next, the code uses the
ContentSchedule property. The first item within the full schedule array will contain the date the post is scheduled to go live. With this data, you can then create a list and order it by this date within descending order to get 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!