Search code examples
ssisezapi

SSIS, EzAPI, Template


I am using the EzAPI to create SSIS packages via .NET, but when I load an existing package as a template that has existing components (sequence containers and execute SQL tasks etc) the EzExec collection is empty, while the DTS Executables collections has many members. I need to reference some of these existing components as parents and precedents to tasks I want to add to the package via the EzAPI.

Am I missing something in the initalization of the package, or is this even possible?

Below is a edited sample of the code I am attempting with the removal of layout info, this is still not working, count of Executables is 7, count of EzExexs is 0.

Thanks, Andrew

public static EzPackage loadPackageTemplate(string templateLocation)
{
    EzPackage ezPackage = new EzPackage();
    try
    {
        StreamReader s = new StreamReader(templateLocation);
        string templateContents = s.ReadToEnd();
        s.Close();

        templateContents = removeLayoutInformation(templateContents);
        ezPackage.LoadFromXML(templateContents);
    }
    catch (Exception)
    {
        throw;
    }

    //need to remove layout from template
    return ezPackage;
}

public static string removeLayoutInformation(string strXML)
{
    try
    {
        //Remove the layout information.
        while (strXML.IndexOf("<DTS:PackageVariable>") > -1)
        {
            strXML = strXML.Remove(strXML.IndexOf("<DTS:PackageVariable>"), strXML.IndexOf("</DTS:PackageVariable>") - strXML.IndexOf("<DTS:PackageVariable>") + 22);
        }
    }
    catch (Exception)
    {
        throw;
    }

    return strXML;
}

public static EzExecutable GetExecutable(EzPackage ezPac, string identifier)
{
    EzExecutable toReturn = null;

    foreach (EzExecutable ezEx in ezPac.EzExecs)
    {
        if (ezEx.EzName == identifier)
        {
            toReturn = ezEx;
            break;
        }
    }

    return toReturn;
}

EzPackage pac = SSISGen.loadPackageTemplate(@"C:\Temp\SSISPackageTemplates\LoadFact.dtsx");

Solution

  • The problem is the layout data throws off the API. There's a discussion on the Codeplex site covering this problem. The poser, Josh Robinson, also blogged about his experience.

    Anyways, the crazy thing about SSIS, the layout stuff BIDS/SSDT presents is bolted on to the actual package markup. That interferes with the ezapi stuff so the fix is to strip it out as Josh demonstrates.

    Code copied here for future preservation

    //Save the package object to XML
    string strXML = null;
    strXML = TestPackage.SaveToXML();
    
    //Count instances of existing SSIS layout code in package.
    int LayoutCount = Regex.Matches(strXML, "<DTS:PackageVariable>").Count;
    
    //Remove the layout information.
    for (int i = 0; i < LayoutCount; i++)
    {
        strXML = strXML.Remove(strXML.IndexOf("<DTS:PackageVariable>"), strXML.IndexOf("</DTS:PackageVariable>") - strXML.IndexOf("<DTS:PackageVariable>") + 22);
    }