WiX installer with parameter configurations

This blog continues on from the previous WiX/installation/deployment blogs.

In all of these blogs, WiX was used to create the setup. This is a free tool which plugs into visual studio.

When creating a silent installer which will be used for automatic builds, automatic deployments, different end targets require different configurations. This can be implemented using the WiX util:XmlFile functionality.

In the WiX wxs file, the namespace needs to be referenced.

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">

Depending upon your installation requirements, default parameter values can be defined, or not:

<!-- Default configuration values -->
<Property Id="INSTALLPATH">C:\damienbod</Property>
<Property Id="DEFAULT_CONNECTION_STRING">a test connection string...</Property>
<Property Id="CONFIGVALUE_CUSTOMERRORS">Off</Property>

Now you can define any elements which are to be replaced with your parameters:

<util:XmlFile Id='DefaultConnectionString' 
     Action='setValue' 
     ElementPath='/configuration/connectionStrings/add[\[]@name="DefaultConnection"[\]]/@connectionString' 
     File='[INSTALLDIR_SimpleWebApp]web.config' Value='[DEFAULT_CONNECTION_STRING]' />

You can see that the value attribute contains our parameter. The util:XmlFile can do almost any xml rendering required. See the links underneath for documentation.

Here’s the full wxs file:

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
  <Product Id="*" Name="damienbod xml util example" Language="1031" Version="1.0.2.6" Manufacturer="damienbod" UpgradeCode="7f152956-f604-4453-affa-9ea242d3fbe4">
    <Package Id="*" InstallerVersion="305" InstallScope="perMachine" Compressed="yes" />

    <Media Id="1" Cabinet="WiXWebInstaller.cab" EmbedCab="yes" />
    
    <!-- Default configuration values -->
    <Property Id="INSTALLPATH">C:\damienbod</Property>
    <Property Id="DEFAULT_CONNECTION_STRING">a test connection string...</Property>
    <Property Id="CONFIGVALUE_CUSTOMERRORS">Off</Property>

    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id='ProgramFilesFolder' Name='PFiles'>
        <Directory Id='INSTALLPATH' Name='SimpleWebApp' />
      </Directory>
    </Directory>

    <DirectoryRef Id='INSTALLPATH'>
      <Directory Id='INSTALLDIR_SimpleWebApp' Name='SimpleWebAppDeployment' />
    </DirectoryRef>

    
    <Component Id='WebAppSettings' Guid='7f152956-f604-4453-affa-9ea242d3fbe5' Feature='ProductFeature' Directory='INSTALLDIR_SimpleWebApp' >
      
          <util:XmlFile Id='DefaultConnectionString' 
                    Action='setValue' 
                    ElementPath='/configuration/connectionStrings/add[\[]@name="DefaultConnection"[\]]/@connectionString' 
                    File='[INSTALLDIR_SimpleWebApp]web.config' Value='[DEFAULT_CONNECTION_STRING]' />

          <util:XmlFile Id='CustomErrors'
                    Action='setValue'
                    ElementPath='/configuration/system.web/customErrors/@mode'
                    File='[INSTALLDIR_SimpleWebApp]web.config'
                    Value='[CONFIGVALUE_CUSTOMERRORS]' />

          <CreateFolder />
       </Component>

    <Feature Id="ProductFeature" Title="UtilInstaller" Level="1">
      <ComponentGroupRef Id="SimpleWebAppGroup" />
      <ComponentRef Id="WebAppSettings" />
    </Feature>
    
    <MajorUpgrade Schedule="afterInstallInitialize"  AllowDowngrades="no" DowngradeErrorMessage="ok" AllowSameVersionUpgrades="yes" />

  </Product>
</Wix>

Now the installer can be built. If the msi is executed without any parameters, the default values are used.

If the command line is used with parameters, any parameters required can be changed. Here’s an example which changes the INSTALLPATH parameter to “C:\damienbod1”

msiexec /i WiXWebInstaller\bin\Release\WiXWebInstaller.msi INSTALLPATH="C:\damienbod1"

Now everything works.

One improvement is that if you required 20, 50, or 100 different parameters for each end client, this command line command becomes a problem. To make it easily for development and also for end clients, an XML file could be used which contains all the parameter in a reader friendly format. A separate exe or a power script could be used to extract the parameters from the file and then call the msi from the commandline.

code: https://github.com/damienbod/WixInstallerUtilXmlFile

Links:

http://wix.tramontana.co.hu/tutorial/com-expression-syntax-miscellanea/xml

http://blogs.technet.com/b/alexshev/archive/2009/05/27/from-msi-to-wix-part-25-installable-items-updating-xml-files-using-xmlfile.aspx

2 comments

  1. I’m trying to replicate the web.config editing and getting an error which basically says Component is not a valid child element of Product. Any ideas where I’m going wrong? I’m using Wix 3.9

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: