This example shows how to create a new task using an existing XML template for a task definition.
First we need to get some template XML for a task. One method is to define a task XML skeleton as follows:
TaskXMLTemplate = "<Task ID="""" Name="""" Active=""0"" NextEID=""3"" AR=""0"">" & _ "<Schedules/>" & _ "<Source HostID="""" Type="""" Path="""" FolderName="""" FolderID="""" FolderType="""" FileMask="""" DeleteOrig=""0"" NewFilesOnly=""0"" SearchSubdirs=""0"" Unzip=""0"" RetryIfNoFiles=""0"" " & _ "UseDefRetryCount=""1"" UseDefRetryTimeoutSecs=""1"" UseDefRescanSecs=""1"" UseDefBlindDL=""1"" UDMxFi=""1"" UDMxBy=""1"" UseDefMD5Get=""1"" UseDefMD5File=""1"" UseDefUser=""1"" UseDefClientKey=""1"" UseDefRsm=""1"" ID=""1""/>" & _ "<Destination HostID="""" Type="""" Path="""" FileName=""[OrigName]"" UseOrigName=""1"" ForceDir=""1"" OverwriteOrig=""1"" UseRelativeSubdirs=""0"" Zip=""0"" UseDefRetryCount=""1"" UseDefRetryTimeoutSecs=""1"" ID=""2""/>" & _ "</Task>"
Alternatively, you could have a template task defined in MOVEit Automation from which you can build new tasks:
TaskXMLTemplate = oAPI.GetItemXML("Task", "Template Task", True)
In the following code we alter the template task XML to create a brand new task. The SuggestItemID() function is used to generate a new ID for the task. The CreateItem() function is used to create the new task once done modifying the XML.
ItemType = "Task" ' Get a suggested ID for the task. ItemID = oAPI.SuggestItemID(ItemType) ' Update the TaskXMLTemplate to include the new Task ID and a friendly name. ItemXML = oAPI.SetAttribute(TaskXMLTemplate, "Task", 1, "ID", ItemID) ItemXML = oAPI.SetAttribute(ItemXML, "Task", 1, "Name", "New Task Created by API") ' We will use a MOVEit DMZ server for the Source. SourceType = "Host" SourceHostType = "siLock" SourceName = "My DMZ Host" SourceXML = oAPI.GetItemXML(SourceType, SourceName, True) If SourceXML = "" Then ErrDesc = "Couldn't find host with name '" & SourceName & "'" bOK = False Exit Sub End If ' Get the ID for this Source host. SourceID = oAPI.GetAttribute(SourceXML, SourceHostType, 1, "ID") ' Update the ItemXML with the source information. ItemXML = oAPI.SetAttribute(ItemXML, "Source", 1, "HostID", SourceID) ItemXML = oAPI.SetAttribute(ItemXML, "Source", 1, "Type", SourceHostType) ItemXML = oAPI.SetAttribute(ItemXML, "Source", 1, "FolderName", "/Distribution/Accounting") ItemXML = oAPI.SetAttribute(ItemXML, "Source", 1, "FileMask", "*.xls") ' We will use an FTP server for the Destination. DestType = "Host" DestHostType = "FTP" DestName = "My FTP Host" DestXML = oAPI.GetItemXML(DestType, DestName, True) If DestXML = "" Then ErrDesc = "Couldn't find host with name '" & DestName & "'" bOK = False Exit Sub End If ' Get the ID for this Destination host. DestID = oAPI.GetAttribute(DestXML, DestHostType, 1, "ID") ' Update the ItemXML with the destination information. ItemXML = oAPI.SetAttribute(ItemXML, "Destination", 1, "HostID", DestID) ItemXML = oAPI.SetAttribute(ItemXML, "Destination", 1, "Type", DestHostType) ItemXML = oAPI.SetAttribute(ItemXML, "Destination", 1, "Path", "/inbound") ' Attempt to add the new item to some task groups. Groups = "My Tasks|Production Tasks|DMZ Tasks" ' Since we're creating an brand new item, there's no need to call CheckoutItem(). ' Submit the new item using CreateItem(). bOK = oAPI.CreateItem(ItemType, ItemID, ItemXML, Groups) If Not bOK Then ErrDesc = "Error creating item: " & oAPI.GetErrorCode() & ": " & oAPI.GetErrorDescription() End If