Automating Business Process with Sitecore Scheduled Tasks

Automating Business Process with Sitecore Scheduled Tasks

If you are a professional Sitecore Developer I am sure that at certain stage you asked yourself if there is a feature to perform certain tasks at a specific schedule.

Still if you have not thought about this your clients will ask you about it at certain stage 🙂

Sitecore Database Tasks feature allows you to create tasks able to be executed at certain Date and Time.

With this feature automating your business process becomes really flexible and powerful.

Based on MY experience a very common task clients ask is to schedule items publishing.

So let us create a Task to publish a certain group of items at a specific schedule.

As usual I always like to keep a good and effective didactic so below is a very rough, and self-explanatory, top view of how this works:

We will create a Task on Sitecore that calls a Command which points to a .net Class which contains the publishing logic.

 

1.

Firstly, let us create our .net class with the publishing logic:

Implementation:

public class ItemsPublishingTask
{
    public void Execute(Item[] itemArray, CommandItem commandItem, ScheduleItem scheduleItem)
    {
        foreach (Item item in itemArray)
        {
            PublishItem(item);
        }
    }


    public void PublishItem(Item item)
    {
        try
        {
            PublishOptions p = new PublishOptions(Sitecore.Data.Database.GetDatabase("master"), Sitecore.Data.Database.GetDatabase("web"), PublishMode.SingleItem, Sitecore.Context.Language, DateTime.Now);
            p.RootItem = item;
            p.Deep = true; // Publishing subitems

            (new Publisher(p)).Publish();
        }
        catch (Exception e)
        {
            Sitecore.Diagnostics.Log.Error("Exception, PublishItem: " + e.Message, this);
        }
    }
}

 

2.

Then let us create a Command named ExecuteItemsPublishing.

On Sitecore Content Editor go to /sitecore/system/Tasks/Commands right lick on it and insert the Command with the following settings:

Where Type is the [namespace, assembly] and Method is the method we want to be executed.

 

3.

Next let us create the Database Scheduled Task which contains the schedule information.

Right click on Schedules folder and insert a new Scheduled Task named ItemsPublishing.

Command: Set Command field to ExecuteItemsPublishing.

Items:  You can pass an array of items to your method by placing any XPath here.  Items can also be divided by pipe separator.

For example: /Sitecore/content/Home/Item1 | /Sitecore/content/Home/Item2

Schedule: In the Schedule field you can place 4 parameters divided by pipe separators:
– From Date
– To Date
– Day of Weeks. For Monday and Saturday you should write 66 (2+64)

public enum DaysOfWeek
{
None = 0,
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64,
}

 – Period of time e.g.: 01:00:00 – every 1 hour.

 

In the example above I have set up the Schedule to be execute on Monday, 01 July 2018 at 1AM and expire on same date. I have also set this task to be auto removed after execution.

Please note that you could set a long period for this task to be recurring until reaches the expiration date.

Last Run: After executing the task Sitecore automatically updates this filter.

Async: Run the task asynchronously.

Auto remove: Remove the task after invoke it. This setting works after the expiration data in the Scheduled field. e.g.? if the expiration data is after 2 weeks then after 2 weeks the task will be removed.

As you can see, the possibility to execute your .net class to a specific schedule makes this feature really powerful. The items publishing is just one very common example but you can write your code to do any business logic you want. Please keep in mind that publishing clears the cache and it could potentially affect the performance of the website.

If you have any questions or comments about this please feel free to ask and I will be glad to help 🙂

 

 

Best Regards

 

Leave a Comment

%d bloggers like this: