24Mar, 2021
Optional Parameter Don't Play Nice With Sitecore Scheduling
In my post, How to Add Custom Buttons to the Content Editor Ribbon, I showed a snippet near the end that executes an import function. mnpImport.Run() is called when this custom button is hit.
public class RunImportImmediately : Command { public override void Execute(CommandContext context) { MNPImport mnpImport = new MNPImport(); mnpImport.Run(); SheerResponse.Alert("MNP import task has completed.", new string[0]); return; } }
Works great! But, I eventually added an agent to run this method every three hours, and I wanted the logs to indicate whether or not it was executed manually vs on a schedule. The optional parameter isManual was added and used inside Run() like so:
if (isManual) TaskSummary.AppendLine($"{DateTime.Now.ToString(@"h\:mm\:ss tt")} - Task manually triggered by User {User.Current.Name}.");
One small problem I came across, is that an agent won't work well with a method that has optional parameters. Refactoring the code so the private method RunTask is eventually executed solved the problem, so the parameter was no longer needed, but this can be filed under gotchas for sure.
public bool Run() return RunTask(false);