Search This Blog

Saturday, October 24, 2009

Using VB to show IIS sites within Installshield 2010

Quite often I have worked on projects where an installer is needed.  So far I am quite familiar with Installshield and its inner workings so this blog describes a simple piece of code which solves a generic issue I had.

More often than not we program in a web based world, a web based installer is needed and the obvious incline is to use IIS, well at least for me considering I am mainly of a .Net developer.  In Installshield everything is easy when it comes to installing something on the local IIS but it gets complicated when you want to customise this process and allow users to choose the website within which the application will be installed or even the name of the application.

Customising the name of the application is easy and can be done simply by using what Installshield refers to as “properties”.  Expose a simple edit box and then in your “Internet Information Services” node simply change the “Name” to point to the property mapped to the edit box (e.g. “[MY_VIRTUAL_DIRECTORY]”).

Things get complicated when you want to show the user a list of websites to which your application can be added to, by default it goes to the “Default Web Site” or at least that’s what I assume but not always as stated by this table here.

Saying that, there is no easy way of showing the user a list of already existing websites on the local computer so after a bit of hunting around I found several solutions on the Installshield forums which advised me of a particular VB script which does what we need.

Dim objViewDim objDB
Dim objInstaller
Dim objRecord
Dim objW3SVC, objIisWebSite
Dim lngOrder
Set objDB = Session.DatabaseSet
objInstaller = Session.Installer

'Delete any records that may exist
Set viewList = Database.OpenView("SELECT * FROM ComboBox")
viewList.ExecuteSet recList = viewList.Fetch

'delete any existing records
While Not (recList Is Nothing)
viewList.Modify 6, recList ' 6 = deleteSet
recList = viewList.Fetch
Wend
viewList.CloseSet

objView = objDB.OpenView("select * from ComboBox")

' enumerate webservers on localhost and populate ComboBox tableSet
objW3SVC = GetObject("IIS://localhost/W3SVC")
lngOrder = 1


For Each objIisWebSite In objW3SVC
If objIisWebSite.Class = "IIsWebServer" Then
If LCase(objIisWebSite.ServerComment) <> "administration web site" Then
' add site name to ComboBox table (which is used by our dialog ComboBox)
Set objRecord = objInstaller.CreateRecord(4)
objRecord.StringData(1) = "WEBSITE_DROPDOWN" ' property name
objRecord.IntegerData(2) = lngOrder ' order
objRecord.StringData(3) = objIisWebSite.Name ' value
objRecord.StringData(4) = objIisWebSite.ServerComment ' text
Call objView.Execute(objRecord) ' now add the record to the table
Call objView.Modify(7, objRecord) ' (7 = msiViewModifyInsertTemporary)

lngOrder = lngOrder + 1
End If
End If
Next
Call objView.Close

Note that the value of the combobox is “objIisWebSite.Name” and you might think this is the human readable string which appears as the website name, but its not.  The name of the website is a unique number (ID) assigned to it as shown in the figure below:

image

Therefore when you want to specify where your application will be added to, you have to modify the “Default Web Site” node (which you will add as the default behaviour and put all your applications under it).  A bit counter intuitive as earlier I simply thought that instead of naming it “Default Web Site” I have to map it to my property which corresponds to the drop down list, i.e. "WEBSITE_DROPDOWN”.  But the trick is to associate the “WEBSITE_DROPDOWN” property to the “Site Number” setting in the Default Web Site node.

5 comments:

  1. Whats up man. Your blog posts seem to have dried up a bit lately!

    ReplyDelete
  2. I don't see a lot of activity on your blog here. I deperately need to get this working. I followed your instructions and the UI elements do work perfectly. However I can't get the installer to install to my selected site! It keeps going to the default site no matter what I do. Please help!
    Riaan

    ReplyDelete
  3. Have you guys changed the site id in the web site properties? It defaults to 0, try setting it to something like 9999

    ReplyDelete
  4. Sorry, I was on a bit of a hiatus, hope you got this working?

    ReplyDelete