Sitecore – Task Schedulers

This is a 3 part post, where we will be creating a Sitecore Task Scheduler to read RSS Feed and create item in the sitecore item and finally, sort the created items using a custom Publish Date field.

Part 1 – Sitecore Task Scheduler

Sitecore has a build in Task Scheduling Agent that schedules predefined/build in tasks at the specified intervals.

Before discussing about more about this concepts or features I would like cite you how it can be put in to use.

Suppose you need a logic that needs to run at a specific time in a specific interval (Like a routine) like cleaning a database, posting contents to Web API or Sending a report  etc, Sitecore Task Scheduler are to the rescue.

Let’s look on how we can read a RSS Feed at a specific interval daily and create Items in Sitecore Content Tree and publish them using a Custom Task Scheduler which can then be presented in the website based on the UI.

For every Task Scheduler to be created an Run, we need three components.

  1. Command — This is the Logic(the code) that needs to be executed every time when the scheduler schedules the TASK
  2. Items – This is the item that will be passed as an Argument to the logic and our operations can be carried out in this item.(Item can be subjected to CRUD)
  3. Schedulers — This has the data on when a command needs to be called, at what time interval and has the data on when it’s Last Run.

All these components can be found under – /sitecore/system/Tasks — It is recommended to create Custom Tasks under this content so that it will be easy to manage.

Command: Create a custom command under /sitecore/system/Tasks/Commands/Your Project/Command using the system command template – /sitecore/templates/System/Tasks/Command

  • The created item will have two fields – Type and Method.
    • Type  – refers to your Namespace with Class Name and Assembly in should be provided in the format (MySitecore.Features.NewsFeed.Tasks.SyncRSSFeed,MySitecore.Features.NewsFeed)
    • Method – the method name in your class to which the control will be passed.

Scheduler: Create a Custom Scheduler Item under /sitecore/system/Tasks/Schedules/Your Project/Scheduler using the system Scheduler template – /sitecore/templates/System/Tasks/Schedule

  • The Schedule Item has the following key fields
    • Command – It’s of DropTree type with the Command Folder as it’s source. You can select the command item created from the DropDown
    • Items – Map the Item that needs to be passed to the custom logic
    • Schedule –  The Interval in which the task needs to be run it should be specified in the following format –

{start timestamp} | {end timestamp} | {days to run bit pattern} |{interval}

Start/End Timestamp Format – YYYYMMDD in this example it’s 20190816 and I don’t have an end date so I have set to the maximum – 99990101

  • The Day are set in bit pattern. With a bit value for each day of the week.
    • Sunday – 1
    • Monday – 2
    • Tuesday – 4
    • Wednesday – 8
    • Thursday – 16
    • Friday – 32
    • Saturday – 64
  • So if mention the sum of the days for which the tasks needs to be run for example, if it needs to be run every Monday set the value to 2. If it needs to be run every Monday and Tuesday (2+4) set the value to be 6
  • If it needs to be run everyday – sum of all the days – 127 would be the value. In this example I have set it to 127
  • Interval = HH:MM:SS 01:00:00 means the task will run every one hour. 00:02:00 means the task will run every 2 minutes.
  • Last run –  Indicates the value to last run date/time

Part – 2  Read RSS Feed and Create Items based on RSS Item template programmatically.

Now to the Custom Class (Mentioned in the Command Item – Type field). You can check creating the Sitecore command/schedule items and creating the class with Execute method and specifying it’s information to the created Sitecore item. To check simply put in a log and check the log file whether the log text is present.

We will work on the logic for reading the RSS Feed and creating Items in Sitecore based on the RSS Item Template. I have created a RSSItem in Sitecore with the following fields.

RSSItem

RSS Feed:

The idea is to have a Folder Item (RSS) that will be passed as an input to the Task Scheduler. This RSS Folder will have sub items of type RSS Feed. The RSS Feed  has two fields – Name and the Feed URL. Based on the feed URL, RSS Item will be created as a sub item under the corresponding RSS Feed.

In a nutshell, the final folder will be something  like

Now we will add logic to read RSS Feed and Create RSS Item in Sitecore whenever this Task is RUN.

Part – 3 Custom Sort – Based on the PubDate field

Sitecore generally sorts sub items based on the item name. It can also be sorted based on other parameters like Created, Updated, Reverse etc all these are OOTB.

After reading the Feed and creating the RSS Items under RSS Feed, the created items will be sorted by the RSS Item Name. While setting this as a data source to a rendering the latest RSS Item from the RSS Feed wont be displayed as sub items are sorted based on their title.

But while displaying the feed in our website, it would be useful only if the latest items are displayed first. In order to achieve this, we will creating a Custom Sort Approach based on the Published Date by extending the system comparer class.

To Create a custom sort, we need to create a class that extends Sitecore.Data.Comparers.ExtractedKeysComparer.

And we can add our logic by overriding the following three methods, DoCompare,ExtractKey and CompareKeys

DoCompare compares the items based on the field name and returns the sorted item list.

Extract Key Returns the key object (the item and the key-Published Date field value)

Based on the keys, the items will be compared in CompareKeys method.

Now create a Child Sorting Item named Publish Date under /sitecore/system/Settings/Subitems Sorting and update the Type – Class, Assembly

Now the created Child Item Sorting will be listed in the Sub Item sorting option.

Now this Item can be rendered based on your requirement in your website.

One thought on “Sitecore – Task Schedulers

Leave a comment

Design a site like this with WordPress.com
Get started