This example shows how to create a new task using an existing XML template for a task definition.
The main code snippet assumes there is a variable TaskXMLTemplate defined as follows:
String 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:
String TaskXMLTemplate = oAPI.queryItem("Task", "Template Task", true);
In the following code we alter the template task XML to create a brand new task. The suggestItemID() method is used to generate a new ID for the task. The createItem() method is used to create the new task once done modifying the XML.
String ItemType = "Task"; // Get a suggested ID for the task. String ItemID = oAPI.suggestItemID(ItemType); // Update the TaskXMLTemplate to include the new Task ID and a friendly name. String ItemXML = oAPI.updateAttribute(TaskXMLTemplate, "Task", 1, "ID", ItemID); ItemXML = oAPI.updateAttribute(ItemXML, "Task", 1, "Name", "New Task Created by API"); // We will use a MOVEit DMZ server for the Source. String SourceType = "Host"; String SourceHostType = "siLock"; String SourceName = "My DMZ Host"; String SourceXML = oAPI.queryItem(SourceType, SourceName, true); if(SourceXML=="") { ShowMsg("Couldn't find host with name '" + SourceName + "'"); return false; } // Get the ID for this Source host. String SourceID = oAPI.queryAttribute(SourceXML, SourceHostType, 1, "ID"); // Update the ItemXML with the source information. // Note that siLock (MOVEIt DMZ) hosts use the "FolderName" attribute instead of "Path". ItemXML = oAPI.updateAttribute(ItemXML, "Source", 1, "HostID", SourceID); ItemXML = oAPI.updateAttribute(ItemXML, "Source", 1, "Type", SourceHostType); ItemXML = oAPI.updateAttribute(ItemXML, "Source", 1, "FolderName", "/Distribution/Accounting"); ItemXML = oAPI.updateAttribute(ItemXML, "Source", 1, "FileMask", "*.xls"); // We will use an FTP server for the Destination. String DestType = "Host"; String DestHostType = "FTP"; String DestName = "My FTP Host"; String DestXML = oAPI.queryItem(DestType, DestName, true); if(DestXML=="") { ShowMsg("Couldn't find host with name '" + DestName + "'"); return false; } // Get the ID for this Destination host. String DestID = oAPI.queryAttribute(DestXML, DestHostType, 1, "ID"); // Update the ItemXML with the destination information. ItemXML = oAPI.updateAttribute(ItemXML, "Destination", 1, "HostID", DestID); ItemXML = oAPI.updateAttribute(ItemXML, "Destination", 1, "Type", DestHostType); ItemXML = oAPI.updateAttribute(ItemXML, "Destination", 1, "Path", "/inbound"); // Attempt to add the new item to some task groups. String 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(!bOK) { ShowMsg("Failed to create item: " + oAPI.getErrorCode() + ": " + oAPI.getErrorDescription()); }