Tuesday, November 17, 2015

Schedule code to run using Agents in Sitecore

In Sitecore the concept of an agent is simply a scheduled task that is managed by Sitecore, which will call a given method on a class. By default Sitecore will already have several agents defined in the web.config file, one example is an agent scheduled every 4 hours to clean up the publishing queue.

A specific example where I have used agents, was to schedule a custom class which synced data into Sitecore from a third party system.


Defining the Sitecore Agent

In your web.config file, locate the /configuration/sitecore/scheduling/ section, here you will notice several agent items. Insert a new agent into this section as follows:
<agent type="MyProject.Jobs.DataSync" method="DoSync" interval="06:00:00"  />
In this example, the DoSync method of the MyProject.Jobs.DataSync class will run every 6 hours.

Defining the class

The example class for the above agent would look like follows:


Using parameters

It's also possible to pass parameters into the class you are calling, this would be done using the following example.
<agent type="MyProject.Jobs.DataSync" method="DoSync" interval="06:00:00">
  <param>Test Value</param>
</agent>

No comments:

Post a Comment