Schedule a Sitecore Custom Agent

Sitecore agents are agents which execute a specific task. For example, the Cleanup Publish Queue agent is scheduled to run every 4hrs (default value) to remove records from the Publish Queue tables.

Sitecore Custom Agent

Sitecore custom agent can be defined as an agent that will trigger a job for your specific needs. Some possible agents that you can create:

  1. Auto-Publish Agent.
  2. Batch Data Import Agent.
  3. Batch Cleanup Agent.

You can easily create a custom agent to trigger your specific task. In this post, I will explain how to implement a custom agent. I will take an example of a Batch Item Modifier Agent. The agent will have the task to update items under a specific node, which will be provided in the configuration file, and update a field called Update by Batch Agent with the actual time and date.

First thing is to create the agent. Create a class and name it DataModifierBatch and write a method named Run().

public class DataModifierBatch
{
    public string Paths { get; set; }
    
    public Database MasterDatabase
    {
        get { return Factory.GetDatabase("master"); }
    }

    public void Run()
    {
        var parentNodes = this.Paths.Split(',');

        foreach (var parentItem in parentNodes.Select(parentNode => this.MasterDatabase.Items.GetItem(parentNode)))
        {
            if (parentItem == null)
            {
                Log.Error("Parent Node not found", this);

                continue;
            }

            var children = parentItem.GetChildren().ToList();

            foreach (var child in children)
            {
                using (new SecurityDisabler())
                {
                    child.Editing.BeginEdit();

                    child.Fields["Update by Batch Agent"].Value = "Hishaam";

                    child.Editing.EndEdit();

                    child.Editing.AcceptChanges();
                }
            }
        }
    }
}

Now, we need to create a configuration file which will specify the namespace of our newly create agent and the different parameters.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <scheduling>
      <agent type="YourNamespace.DataModifierBatch, YourAssemblyName" method="Run" name ="Data Modifier Batch" interval="04:00:00">
        <!-- The sites names comma separated-->
        <sites>/sitecore/content/home,/sitecore/content/example</sites>
      </agent>
    </scheduling>
  </sitecore>
</configuration> 

Place the configuration file in the Include folder. You can specify any time interval and paths that you desire.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s