Install a New Virtual Directory to Default Web Site with WiX

15 Comments January 25, 2011


Sometimes the internet is totally awesome and other times it’s a complete morass of “how come I can’t find anything?” Recently, I needed to build a quick installer that did nothing more than install new virtual directory under the ubiquitous Default Web Site. I thought installing a basic web site would have been a very common task and was sure there were fifty to sixty examples out there in the blogsphere. However, the internet failed me, which has to be the first time in recorded history that’s ever happened.

Fortunately, there was enough documentation out there that pointed me in the right direction. What’s below is the core .WXS file for an installer that meets the following requirements:

· Verifies that IIS is installed

· Finds the physical path for the Default Web Site

· Installs a specifically named virtual directory under Default Web Site

· Installs the files into a named directory under the Default Web Site physical path

As you can see, the WiX 3.5 code isn’t that exciting, but I thought it would be useful for new WiX developers to see and better yet, steal. Here’s the download for the entire project.

 

<?xml version="1.0" encoding="UTF-8"?>
<
Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
     xmlns:iis="http://schemas.microsoft.com/wix/IIsExtension"
>
  <!--
The upgrade GUID ** DO NOT CHANGE!! **
-->
  <?
define ProductUpgradeCode="da7c5352-634c-408c-ad5c-5ff806106378"
?>
  <!--
The product version.
-->
  <?
define InstallVersion="1.0.0.0"
?>
  <!--
It's aways a major upgrade here.
-->
  <
Product Id="*"
           Name="Basic Web App Install Example"
           Language="1033"
           Version="$(var.InstallVersion)"
           Manufacturer="John Robbins/Wintellect"
           UpgradeCode="$(var.ProductUpgradeCode)"
>
    <
Package Id="*"
             Description="A simple web site installation"
             Comments="Just showing how it works."
             Manufacturer="John Robbins/Wintellect"
             InstallerVersion="300"
             Languages="1033"
             Compressed="yes"
             SummaryCodepage="1252"
             InstallPrivileges="elevated"/>
 
    <!-- Major upgrade checks.
-->
    <
MajorUpgrade Schedule="afterInstallInitialize"
                  DowngradeErrorMessage="A later version of [ProductName] is 
                                    already installed. Setup will now exit.
"/>
 
    <!-- Check to see if IIS is installed. It it's not, error out.
-->
    <
Property Id="IIS_MAJOR_VERSION"
>
      <
RegistrySearch Id="CheckIISVersion"
 
                      
Root="HKLM"
 
                     
Key="SOFTWARE\Microsoft\InetStp"
                      Name="MajorVersion"
 
                     Type="raw"
/>
    </
Property>
 
    <Condition Message="IIS must be installed">
      Installed OR IIS_MAJOR_VERSION
    </Condition>
 
    <!--
Go find the IIS root directory from the registry. On most machines
         that defaults to C:\inetpub\wwwroot. This will be the directory we
         install into.
-->
    <
Property Id="IISROOT"
>
      <
RegistrySearch Id="IISROOT"
                      Type="directory"
                      Root="HKLM"
                      Key="Software\Microsoft\InetStp"
                      Name="PathWWWRoot"
/>
    </
Property>
 
    <Condition Message="IIS does not appear to be installed correctly, the 
                       root directory is not set.
">
      Installed OR IISROOT
    </Condition>
 
    <!-- Describe the media source (you always have to have this)
-->
    <
Media Id="1" Cabinet="media1.cab" EmbedCab="yes" />
 
    <!-- The root of life for any installer.
-->
    <
Directory Id='TARGETDIR' Name='SourceDir'
>
      <!--
Install into the ISS root directory we found earlier.
-->
      <
Directory Id="IISROOT" Name='WebDir'
>
        <!--
Here's this installers install location.
-->
        <
Directory Id='INSTALLDIR'
                   Name='BasicWeb'>
 
          <!-- The component to define the Virtual Directory.
-->
          <
Component Id="WebVirtualDirComponent"
                     Guid="D814F88F-6E0C-4365-A411-2F9807522C3D"
>
            <!--
The virtual directory we are installing.
-->
            <!--
The Alias attribute is the name thata will be put into IIS.
-->
            <!--
The Directory attribute is the "Physical Path" property in
                  IIS and needs to tie to an ID specified in the setup.
-->
            <!--
The WebSite attribute ties to a <WebSite> element in the
                 setup file. As this is an example of installing into the
                 "Default Web Site" that element is not under a component.
-->
            <
iis:WebVirtualDir Id="VDir"
                               Alias="BasicWeb"
                               Directory="INSTALLDIR"
                               WebSite="DefaultWebSite"
>
              <!--
Turn the Virtual Directory into a web application.
-->
              <
iis:WebApplication Id="TestWebApplication"
                                  Name="BasicWeb"
/>
            </
iis:WebVirtualDir
>
            <!--
This is pretty important. If the CreateFolder isn't there the
                 WebVirtualDir won't get created as there's no files in this
                 component.
                 http://www.mail-archive.com/wix-users@lists.sourceforge.net/msg03483.html
-->
            <
CreateFolder
/>
          </
Component
>
        </
Directory
>
      </
Directory
>
    </
Directory>
 
    <!--
Because I want to show an example of installing a web site under an
         existing web site, "Default Web Site", you have to keep this element
         outside of a component. See the WiX documentation:
         http://wix.sourceforge.net/manual-wix3/iis_xsd_website.htm.
         Basically, outside a component means it's a locator/searching. Inside
         a component means it's a creator.
-->
    <
iis:WebSite Id='DefaultWebSite'
                 Description='Default Web Site'
                 Directory='INSTALLDIR'
>
      <!--
This element has to be here or WiX does not compile. It's ignored
           in this case.
-->
      <
iis:WebAddress Id="AllUnassigned" Port="80"
/>
    </
iis:WebSite>
 
 

    <Feature Id='TestProductFeature' Title='Wix File Product Feature' Level='1'
>
      <
ComponentRef Id='WebVirtualDirComponent'
/>
      <
ComponentGroupRef Id='BASICWEBAPPFILES'
/>
    </
Feature
>
  </
Product
>
</
Wix
>

 


15 Comments

  • Gravatar Image
    Jason Haley January 26, 2011 6:18 AM

    Interesting Finds: January 26, 2011

  • Gravatar Image
    Nuhman February 9, 2011 11:46 AM

    Thank you for the script

  • Gravatar Image
    Juan Pablo February 22, 2011 8:25 AM

    Hi,

    I tried to create the virtual directory but it shows me an error with the metadata: failed to write data to metabase key and the installers does not work.
    Do you know why this can be happening?
    Thank you so much for your help

  • Gravatar Image
    John Robbins' Blog February 23, 2011 1:01 AM

    If all you need to do is install your web application into Default Web Site, life is easy. Especially

  • Gravatar Image
    xubo March 30, 2011 2:16 AM

    HI:
    I build this project ,and show me below message:
    unhandled extension element iis:webVirtual Dir .

    Do you know why?

    Thank you so much for your help

  • Gravatar Image
    jrobbins April 13, 2011 4:01 AM

    xubo,

    You need to reference WiXIISUtil.dll.

    Hope it helps!
    - John Robbins

  • Gravatar Image
    mhu September 29, 2011 1:44 AM

    Is it possible to make the directory dynamically so that there is an edit box on installer where user enters the directory name. That is read to the property and finally to the folder names. I have tried but can get the folder name come right. Instead of property value the property string comes to the name. for example [IIS_DIR]

  • Gravatar Image
    mhu September 29, 2011 5:08 AM

    Addition to latest comment: meaning that BasicWeb directory would be created dynamically

  • Gravatar Image
    Yatish November 14, 2011 10:09 AM

    There is a problem with this code when you have more than one website starting with the same name. For Example, I have two websites with name "Website1" and "Website" then even if I select second website "Website", the first website "Website1" will get selected, if "Website1" was created first, i.e. id of "Website1" is less than the id of "Website".

  • Gravatar Image
    FARHAN November 29, 2011 6:25 AM

    How to disable Default Website in iss using wix and adding new "website"
    not application???

  • Gravatar Image
    Daniel Lidstr&amp;#246;m September 11, 2012 5:02 AM

    Sir, you saved my day. Thanks!

  • Gravatar Image
    Ruben Segura November 15, 2012 11:16 AM

    In which line or instruction, sets the application or project that needs to be added to the Installer. Thanks

  • Gravatar Image
    Sanjeev Sharma August 16, 2013 9:36 AM

    It is awesome...You Saved a lot of time....Cheers!!!

  • Gravatar Image
    Krishna December 11, 2013 11:33 AM

    Hi,
    All !!
    I have successfully created setup for MVC Web Application using wix toolset, it is working fine under Default Web Site.
    I have no issue under Default Web Site, but can any one help me if i want to create new website with some other port no rather than creating website under Default website in IIS..

    i.e.: IIS>> Sites>> Default Web site >>My MVC App is working fine.

    Problem in below structure::
    i.e.: IIS>> Sites>> My MVC App


    Thanks in Advance!!

  • Gravatar Image
    Santhosh February 26, 2014 12:39 PM

    Thanks for the code. It sounds good.

Have a Comment?

Archives

Blogs