3

I want to pass class object parameter to the Run method of the scheduling agent in the Sitecore.

Can I pass object parameter to the method and how?

How can I configure param in the config file?

Class:

public class ImportProduct
{
    public void Run()
    {
        //Scheduling Agent calling
    }
}

Config:

<scheduling>
    <agent type="******.SchedulingAgent.ImportProduct" method="Run" interval="00:02:00">
    </agent>
</scheduling>

2 Answers 2

2

You cannot pass a parameter to Run() method but you can

  • set a property on your job class,
  • or pass constructor parameter, assign to a field and use it in your code.

See the UrlAgent class configuration:

<agent type="Sitecore.Tasks.UrlAgent" method="Run" interval="00:15:00">
  <param desc="url">/sitecore/service/keepalive.aspx</param>
  <LogActivity>true</LogActivity>
</agent>

and how it's used in the class:

public class UrlAgent
{
  private readonly string _url;
  private bool _logActivity = true;

  public UrlAgent(string url)
  {
    this._url = url;
  }

  public bool LogActivity
  {
    get => this._logActivity;
    set => this._logActivity = value;
  }

  public void Run()
  {
      string fullUrl = WebUtil.GetFullUrl(this._url);
      if (this.LogActivity)
      {
          ...
      }
  }
0

You can pass parameters in a constructor as Sitecore already uses in PublishAgent.

Config:

<agent type="Sitecore.Tasks.PublishAgent" method="Run" interval="00:00:00" role:require="Standalone or Reporting or ContentManagement">
      <param desc="source database">master</param>
      <param desc="target database">web</param>
      <param desc="mode (full or smart or incremental)">incremental</param>
      <param desc="languages">en, da</param>
    </agent>

Class

public class PublishAgent
{
    private readonly List<Language> _languages;

    private readonly PublishMode _mode;

    private readonly string _sourceDatabase;

    private readonly string _targetDatabase;

    public List<Language> Languages => _languages;

    public PublishMode Mode => _mode;

    public string SourceDatabase => _sourceDatabase;

    public string TargetDatabase => _targetDatabase;

    public PublishAgent(string sourceDatabase, string targetDatabase, string mode, string languages)
    {
        _sourceDatabase = sourceDatabase;
        _targetDatabase = targetDatabase;
        _languages = ParseLanguages(languages);
        _mode = ParseMode(mode);
    }

    public void Run()
    {
        //You can read those parameters here and do your custom code.
        Database database = Factory.GetDatabase(_sourceDatabase);
        Database database2 = Factory.GetDatabase(_targetDatabase);
    }
}

Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.