This project has moved. For the latest updates, please go here.

Add task from xml string

Apr 18, 2015 at 6:11 AM
Edited Apr 18, 2015 at 6:11 AM
How i can add task from xml string ? Not from xml file. I am going to get xml tasks from ms sql database.
Coordinator
Apr 20, 2015 at 4:11 PM
You will get a TaskFolder instance for the folder in which you want to place the task, and then call the RegisterTask method setting the second parameter to the XML text string that contains the task's definition in the schema defined at https://msdn.microsoft.com/en-us/library/windows/desktop/aa383609(v=vs.85).aspx.
Marked as answer by dahall on 4/20/2015 at 8:11 AM
Apr 22, 2015 at 4:10 PM
Edited Apr 22, 2015 at 4:37 PM
What is my mistake ?
  using (TaskService ts = new TaskService())
              {
                  string Task_Settings = System.IO.File.ReadAllText(@"D:\DailyTrigger.xml");

                  TaskFolder tf = ts.RootFolder;
                  tf.RegisterTask(@"XMLTask", Task_Settings);
              }  
My xml file works successfully with method NewTaskFromFile.
Coordinator
Apr 22, 2015 at 7:04 PM
NewTaskFromFile will give you a TaskDefinition but does not create (register) the task. The problem you may be having is a permissions problem when registering. Are you running the executable from an account that has permissions to create tasks? Are you not running as Administrator and having UAC block your access? Your code is correct if you have permissions. Alternately, you could do all that in one line:
TaskService.Instance.RootFolder.ImportTask("XMLTask", @"D:\DailyTrigger.xml");
If you have permission problems, you may need to supply user information:
using (var ts = new TaskService())
{
   // If user information is in the XML file under the Principal tag, you can use this short form:
   ts.RootFolder.RegisterTaskDefinition("XMLTask", ts.NewTaskFromFile(@"D:\DailyTrigger.xml"));
   // If you need to supply user information at this time use this form. See documentation for detail on TaskLogonType.
   ts.RootFolder.RegisterTaskDefinition("XMLTask", ts.NewTaskFromFile(@"D:\DailyTrigger.xml"),
      TaskCreation.CreateOrUpdate, "username", "password", TaskLogonType.XYZ);
}