Friday, May 18, 2007

K2 Solution Installer

For a project I was doing I needed to create a custom installer to deploy a K2 Solution. The client wanted the solution to install the K2 project hands off.

We have four enviroments we had to deal with Dev, QA, Staging and production. Each has a string table and enviromental specific Process Data - Default values.

We use source safe to store the K2 solution and all of it's components. I wanted to have a seperate location were I stored the final solution from my working copy. I created a seperate folder/Project in source safe and linked all the files I wanted to use in my installation project. Now all I needed to do is checkin my code and get the latest version in my final project location. I could then go back and continue working on the solution without linking directly to my working copy.

For my needs I only had one process to export so I didn't deal with multi-process solution. I created a configuration file for each enviroment that looked something like this


<K2>
     <Solution>K2 Solution\K2WorkFlow.ksn</Solution>
     <StringTable>
          <String Title="Title">Value</String>
     </StringTable>
     <DataFields>
     <DataField Title="Title">
          <Type>String</Type>
          <Value>default</Value>
          <Metadata></Metadata>
     </DataField>
     </DataFields>
     <Groups>
     <Group Name="DOMAIN\GROUP">
          <Admin>0</Admin>
          <Start>1</Start>
          <View>0</View>
          <ViewPart>1</ViewPart>
          <ServerEvent>0</ServerEvent>
     </Group>
     </Groups>
     <Users>
     <User Name="DOMAIN\USERNAME">
          <Admin>0</Admin>
          <Start>1</Start>
          <View>1</View>
          <ViewPart>1</ViewPart>
          <ServerEvent>1</ServerEvent>
     </User>
     </Users>
</K2>


After I had seperate configuration files I had the installer copy the correct configuration file over based on a set of radio button to select different enviroments. I Added all the solution files to the installer these would include .kpr, kcm, .dll, .k2n files in the same structure as they appear in my working solution.

Some of the user input questions we ask during the installation process were K2 Server Name, Process Name, Sql Server Name.

After the installer copies all the files we then launch an installer class to do the heavy lifting. I used the "using K2Studio" in my code and included references to Interop.K2Studio, K2ROM, K2SPUtilities .

Note: You must have k2studio installed on the machine running the installer. The installer uses k2studio.exe to compile and export the process.

Here is the main logic of the K2 installer:

  • We load and parse the Configuration file
  • These are the object I used during export process
    1. K2Studio.Application K2App = new K2Studio.Application();
    2. K2Studio.Project K2Project;
    3. K2Studio.ProcessFolder K2ProcessFolder;
    4. K2Studio.Process K2Process;
    5. K2Studio.Processes K2Processes;
  • K2App.Solution.Open(pathToSolutionFile) - This is defined in my configuration file

  • K2Project = K2App.Solution.Projects.Item(0); - I Select the first K2 Project. Since I only had one.

  • I remove all the servers being using the K2Project.ExporServers Object
  • " K2Project.ExportServers.Remove(0);"

  • I re-add the k2 export server "K2Project.ExportServers.Add(serverName);" - The servername is asked during the installation process.

  • I set the K2ProcessFolder to the object K2Project.ProcessFolder;

  • I then get K2Processes = K2ProcessFolder.Processes; and grab the first item out of there. "K2Processes.Item(0);"

  • I then export all the string entries. I check if each item exists already then update the string value if not then add the string - K2Project.ExportServers.Item(0).StringTable.Add(Title);

  • I export the datafields found in the configuration files
  • newDataField = K2Process.DataFields.Add(Title);
  • Set the Type "newDataField.Type= K2Studio.EnumDataType.TypeString;" we use a
    switch statement to determine what type we are using
  • Set the value "newDataField.Value=MyValue;"
  • set the MetaData "newDataField.MetaData=MetatData"

  • We set the Process Name "K2Process.Name = ProcessName". The process name is asked during the installation process.

  • Then we export the k2 process to the server "K2Process.Export(serverName, ref Result);" and then display the results in a MessageBox.

  • Use created a stored procedure the accepts XML anduses the OPENXML command to parser through my configuration file.
  • I select the latest Process ID "SELECT @PROCID=ID from k2.dbo._ProcSet WHERE Name=@PROCESSNAME"
  • I then delete the _Procuser and _ProcGroups there are related to the process ID
  • Iinsert the Groups into the _ProcGroup.
  • Insert the Users into the _ProcUser table

  • Note: At the end of the installation process I could not get K2Studio.exe to close down. So had to create a function that would shut down the process.

And thats all there is to creating an automated install kit for K2