Create and Configure Scheduler in Sitecore: Part-2
In my previous blog, we had discussed Sitecore Scheduler. In this blog, I am going to discuss with you about How to create a scheduler in Sitecore and schedule it with the PowerShell interface.
First, you need to write a class that will have the logic of functionality you want to execute.
namespace Helixbase.Foundation.Scheduler { public class DataImportScheduler { public void DataImport(Item[] items, Sitecore.Tasks.CommandItem command, Sitecore.Tasks.ScheduleItem schedule) { //Code here } } }
The above piece of code will take three arguments:
- Array of Items
- Task command Item
- Task Schedule Item
I will discuss with you more about the above three parameters in the next blog “Pass Sitecore Items”.
Once you write your logic into .Net Class then you need to create Sitecore Command into Sitecore.
Go to the path: “/sitecore/system/Tasks/Commands” and create a new command item. Right-click on the command and insert a new Command item. I have given it the name “DataImport”.
After creating a new command, you will get the below field options:
Type: In which you will define the Class name with the NameSpace of the project. Here our class name is DataImportScheduler.
Method: Method name in which you will write the logic of your scheduler. Here our method name is DataImport.
now your Command item will look like as below:
In the next step, you need to create a Scheduler Item in Sitecore for the above-defined command. For this go to the path: “/sitecore/system/Tasks/Schedules”. Right-click on the Schedules and insert a new Schedule item. I have given it the name “DataImport”.
After creating a new Schedule item you will get the below field there:
Command: Here you can select the command item which you want to schedule. Click on the Down Arrow button and select the command item. I have selected the previously created DataImport Command Item.
Items: Sitecore Items that you want to pass into your .NET Class. I will discuss this with you in more detail in my next blog.
Schedule: In this, you define the time interval, when you want to execute this scheduler. Its pipe separated and find when the task should execute:
{start date}|{end date}|{days to run }|{time interval}
20040720T235900|20060725T235900|127|01:00:00
In this example, the task will be executed from 20.07.2004 23:59:00 to 25.07.2006 23:59:00, every day (as specified by the number 127) and every 1 hour (as specified by 01:00:00).
In the Schedule field you can place 4 parameters divided by pipe separators:
- From Date
- To Date
- Day of Weeks. Here DaysOfWeek enumeration is used. For Monday and Friday, you should write 34 (2+32)
- Period of time e.g.: 01:00:00 – every 1 hour.
public enum DaysOfWeek { None = 0, Sunday = 1, Monday = 2, Tuesday = 4, Wednesday = 8, Thursday = 16, Friday = 32, Saturday = 64, }
Note: For the above Schedule field details, I have taken reference from the Sitecore Official Document website.
Auto Remove: field is used to delete the schedule item, after its completion. For example: If you set the Auto Remove field then the item will be removed once it is executed.
now your command item will look like as below:
Setup schedule configuration setting:
From the above-defined configuration, you can write your schedule configuration manually in the below format:
Option 1: {start date}|{end date}|{days to run }|{time interval} I have discussed about this above in detail.
Option 2: Here I will let you know the easiest way to schedule tasks. To do the same you need to Install the Sitecore PowerShell extension with your Sitecore Instance. Click on PowerShell with Sitecore and Installation for more details. Once you will install the PowerShell extension you can find one new option i.e., “Scripts” after right-click on your Schedule item as below:
Select Edit Task Schedule to open a dialog to edit the scheduled task. You will find below interface:
You can set date and time into fields named Starts running at and Stops running at. In the Days to run field, you can choose on which day you want to run the task. The Interval field is to specify Timespan in form of Days, Hours, Minutes, and Seconds.
Once you select all the above fields you will get the desired scheduled configuration setting into your schedule field of Schedule task.
Manually Trigger the Scheduler Task: Sometimes we want to trigger the task manually to perform unit testing or debug the code. To do the same again right-click on the Scheduler task and Run Task Schedule option under Script’s option as below:
It will execute your scheduler task without any delay. In continuation of this Scheduler series blog in my next blog, I will discuss with you “Pass Sitecore Items in .Net Class with Scheduler”.
Happy Sitecoreing 😊
Great post
ReplyDelete