There is no way to import only pieces of the job.
See the help section on Managing Multiple Environments for suggestions on how to handle environment differences, especially Configuring Jobs for Multiple Environments. Specifically, you want to define your variables for things that are different between environments at the server level, or in a group that doesn't get replaced during import. For credentials, don't change the job to use a different Credential Profile after you use it; go change the Credential Profile to use different credentials.
In your code you're hard-coding the OID of the job to import. A better way to do that is to use the option to import all/only jobs and other objects that were explicitly selected during export. (Unless for some reason you want to have it hard-coded for a particular job.)
Here's a code sample:
bool ImportFile(Scheduler connection, string fileName, OID remapToGroupOID = null, OID remapFromGroupOID = null)
{
if(null==remapFromGroupOID && null != remapToGroupOID)
{
remapFromGroupOID = WellKnownOIDs.RootGroup;
}
try
{
ImportFileInformation fileInfo = null;
var importer = connection.GetImportExportFacility();
var options = new ImportOptions();
options.ImportSecurity = false;
options.IncludeJobHistory = false;
options.ImportSelectionsOnly = true;
options.PostImportAction = PostImportAction.Save;
importer.SetSourceFromFile(fileName);
if (!importer.ParseImportData(options, out fileInfo))
{
ShowMessages(importer.Messages);
return false;
}
foreach (var item in fileInfo.ObjectHeaders)
{
var objectParms = new ObjectImportParameters(item);
//Don't replace existing objects that were not explicitly selected for export.
if (item.ExplictlyExported)
{
objectParms.ImportAction = ObjectImportAction.Import;
}
else
{
objectParms.ImportAction = ObjectImportAction.Skip;
}
if (null != remapFromGroupOID && null != remapToGroupOID && objectParms.OID.ClassID == ClassID.JobGroup)
{
/*
* Any group that has a parent group of remapFromGroupOID will be added under remapToGroupOID instead.
* That is, the group hierarchy beginning with children of remapFromGroupOID will now appear under remapToGroupOID.
*
* For example, assume this structure exists on the server where Job A is exported.
* Root
* Group 1
* Group 1.1
* Group 1.2
* Group 1.2.1
* Job A
*
* It is imported on a new server with this group structure:
* Root
* Group 2
* Group 2.1
*
* If you map Group 1 to Group 2, you will end up with:
* Root
* Group 2
* Group 2.1
* Group 1.1
* Group 1.2
* Group 1.2.1
* Job A
*
*/
if (objectParms.OID.Equals(remapFromGroupOID))
{
objectParms.MapToOID = remapToGroupOID; //tell the import to replace references to this group with a reference to the new group.
objectParms.ImportAction = ObjectImportAction.Skip; //don't import this group, because nothing will reference it.
}
else if(objectParms.OID.Equals(WellKnownOIDs.RootGroup))
{
objectParms.ImportAction = ObjectImportAction.Skip;
}
}
options.ObjectImportParameters.Add(objectParms);
}
ReadOnlyCollection<ImportedObject> importedObjects = null;
var result = importer.ImportData(options, out importedObjects);
if (result == ImportResult.SaveSucceeded)
{
Console.WriteLine("Import succeeded");
return true;
}
else
{
//import failed for one or more objects.
Console.WriteLine("Import failed");
ShowMessages(importer.Messages);
foreach (var item in importedObjects)
{
if (item.ImportResult != ImportResult.SaveSucceeded)
{
Console.WriteLine("Failed: " + item.Object.GetDescription());
ShowMessages(item.Messages);
}
}
return false;
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
}
void ShowMessages(MessageCollection messages)
{
foreach (var message in messages)
{
Console.WriteLine(message.ToString());
}
}
We're creating an ObjectImportParameters object for each object found in the source file, and we're telling it to import only if the object was explicitly selected during export. That means that any referenced objects that were not explicitly selected are not imported if they already exist. If they don't exist already they are. For example, say you select Job 1 in the export dialog. That job chains to Job 2 and Job 3. If Job 2 and Job 3 don't exist, they'll get imported. But if they do already exist, they'll be skipped.
This sample also includes to remap a group onto a new group. In the Console, you can only remap the entire hierarchy (starting with children of the root group) onto a new node, but this code will let you start at an arbitrary point in the hierarchy. This code will not work right if the groups already exist--you want to use this option the first time you import the groups, to get them where you want them.
Alternatively, you can rearrange the hierarchy in the Console, and it will "stick" as long as you don't re-import the groups. That is, if your code never imports groups that already exist, their hierarchy will never get altered by the import. Or, depending on your needs, you may need to write additional code to rearrange things programmatically after the import.
Note that you do not want to be creating new copies of objects (GenerateNewCopy should always be false). If you generate new copies, then the next time you import those same jobs, the import won't be able to match them to the existing copies. (GenerateNewCopy creates a new OID for the object, and the OID is what the import uses to know if it's importing a new object or updating an existing one.) GenerateNewCopy is really only used if you want to clone a section of the hierarchy once, not when you want to be able to repeatedly update objects.