Scheduled Task TaskData Structure Parsing

jeff McDaniel asked on January 17, 2017 00:52

When creating and scheduling custom tasks what is the suggested way of passing and parsing Task Data? Some built-in tasks available for scheduling seem to have <data></data> passed in. Is Task Data intended to be an XML like string element <data><foo>1</foo><bar>2</bar></data>? TaskData of a TaskInfo object seems to come in as a string as in the linked example.

Is there method available from one of the helpers when creating custom tasks that parses that XML like string?

Thanks!

Recent Answers


Brenden Kehren answered on January 17, 2017 01:13

If you need structured data Jeff, then pass in the XML, otherwise just pass in plain text.

To parse out that XML string you can use namespace System.Xml.Linq.

Here is a sample XML structure in the cms_class table to get fields.

<form version="2">
  <field column="RoleID" columntype="integer" fieldtype="CustomUserControl" isPK="true" system="true" publicfield="false" guid="0c8cc1de-1c82-4596-b6d0-b2c60bcad3b7" reftype="Required">
    <properties>
      <fieldcaption>RoleID</fieldcaption>
    </properties>
    <settings>
      <controlname>labelcontrol</controlname>
    </settings>
  </field>
  <field column="RoleDisplayName" visible="true" columntype="text" fieldtype="CustomUserControl" system="true" columnsize="100" publicfield="false" guid="c5f83fe3-f362-431f-80be-2d40122e469e" translatefield="true" reftype="Required">
    <properties>
      <fieldcaption>Role display name</fieldcaption>
    </properties>
    <settings>
      <controlname>localizabletextbox</controlname>
      <ValueIsContent>False</ValueIsContent>
    </settings>
  </field>
</form>

I use it something like this when I get the ClassFormDefinition out of the cms.class table.

var doc = XDocument.Parse(dci.ClassFormDefinition);
// get a list of all the tables properties by column value
List<string> elements = doc.Descendants("field").Select(el => el.Attribute("column").Value).ToList();

// got the class properties now compare
foreach (string s in elements)
{
    // see if the class info contains the column in the data row passed in this method
    if (Row.Table.Columns.Contains(s))
    {
        // contains the column so set a value
        newCustomTableItem.SetValue(s, Row[s]);
        okInsert = true;
    }
}

This code gets all the <field> and gets the column attribute value and puts them into a list of strings. Then I compare that list to a list of columns in a custom table and if they match, then I map the values.

So maybe a bit overkill for what you're looking for but a good example none the less.

0 votesVote for this answer Mark as a Correct answer

Kristian Bortnik answered on January 17, 2017 01:21

TaskData is simply a string. It can contain any data you need.

It's a good idea to keep some sort of structured data, and if XML is your format of choice, you can use the built-in ContainerCustomData class to serialise/deserialise to XML.

public string Execute(TaskInfo task) {
    var taskData = new ContainerCustomData();

    // Loading the XML data from TaskData
    var taskData = taskData.LoadData(task.TaskData);

    // Accessing the column values
    // taskData["columnName"]

    if(taskData["foo"] == "bar")
    {
       // ...
    }
}

The expected XML format is along the lines of:

<CustomData>
    <foo>bar</foo>
    <columnName>value</columnName>
</CustomData>
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.