How to add a new trigger to an existing task?

May 12, 2009 at 12:34 PM

Hello!

I want to edit an existing task.
My code is:
<font size="2">

 

</font>

using (TaskService toTaskService = new TaskService())

{

<font size="2">

 

</font>

Task gr1Task = toTaskService.GetTask("GR1");

<font size="2">

 

</font>

WeeklyTrigger trigger = new WeeklyTrigger();<font size="2">

trigger.DaysOfWeek =

</font>

DaysOfTheWeek.Monday;<font size="2">

trigger.StartBoundary =

</font>

DateTime.Now;

trigger.WeeksInterval = 1;

gr1Task.Definition.Triggers.Add(trigger);

}

This code don't work.

Trigger not added to the task.
Why?

Coordinator
May 15, 2009 at 5:38 PM
Edited Jul 9, 2009 at 6:42 PM

You must grab the TaskDefinition first and then register that definition after adding the trigger. It should be easier and more intuitive, but this mimics the model used when coding directly with the COM interfaces. Use this code, and it should work:

using (TaskService toTaskService = new TaskService())
{
      Task gr1Task = toTaskService.GetTask("GR1");

      // Grab an instance of the definition
      TaskDefinition td = gr1Task.Definition;

      // Add the new trigger to the definition
      td.Triggers.Add(new WeeklyTrigger() { DaysOfWeek = DaysOfTheWeek.Monday, StartBoundary = DateTime.Now, WeeksInterval = 1 });

      // Register the definition
      toTaskService.RootFolder.RegisterTaskDefinition(gr1Task.Path, td, TaskCreation.Update, null, null, td.Principal.LogonType, null);
}