Click here to Skip to main content
Click here to Skip to main content
Go to top

Calling JavaScript from a Visual Studio Installation Project's Custom Action

, 3 Oct 2006
Rate this:
Please Sign up or sign in to vote.
When you create an installation project with Visual Studio, you can use Custom Actions to perform extra processing once the basic installation has completed. This article describes how to have a custom action execute some JavaScript, passing it some parameters.

Introduction

I was recently asked to put together a simple installer for one of our projects. The only installation compiler available was the one that comes with Visual Studio. Whilst limited, this appeared to provide most of the functionality we needed. Any extra work that we needed to perform should have been possible through the "Custom Actions" facility.

Custom Actions allow extra processing to be performed once the basic installation has completed. Most of the examples available show how to call custom actions that you provide in a complied DLL. I wanted to provide extra functionality in a quick and dirty JavaScript file. The documentation said this was possible, but had no examples.

I have written this article to help anybody else coming up against this issue. In particular, how to:

  • Pass parameters to your script;
  • Work with objects such as the File System Object from within your script;
  • Interact with the user.

Executing your script

The first step is to get your script executed. Assuming you have already created a Deployment Project within Visual Studio:

  1. Add your JScript, VBScript, or JavaScript file to the project - use the File System Editor view (right click the project in the Solution Explorer and select View / File System). This means your script file will end up on the target machine.
  2. Run your script as a Custom Action - use the Custom Actions Editor to add the script file under the appropriate action folder. For instance, if you want your script to run after install, add it to the "Install" folder.

Now, when you run the compiled installer, your script should be called.

Passing information to your script

Whilst executing your script is a good step forward, you will probably want to send some data to the script when it is executed. If you highlight your script within the Custom Action Editor, the properties window will have four entries:

  • (Name) - the file name of your script;
  • Condition - a condition that will be tested to determine if your script should be executed or not;
  • CustomActionData - data to be passed to your script when it is executed;
  • SourcePath - source location of your script.

CustomActionData can be used to pass any string information you want to the script as it is executed. The data can include information read from the user. For instance, if you have included one of the "Textbox" dialogs in the User Interface Editor, each text box can be given a property name. The text box contents can be passed to the script by putting the property name in CustomActionData surrounded by square brackets. Other built-in parameters such as TARGETDIR can be supplied in the same way.

One limitation of CustomActionData is that it limits you to a single string. If you want to pass multiple parameters, you are on your own. My solution to this was to delimit my parameters with a "|" character. For example:

[TARGETDIR]|[URL]|[SMTP_ADDRESS]

Within the script, this can be split into separate variables:

var parameters = Session.Property("CustomActionData").split("|"); 
var targetDir = details[0];
var url = details[1];
var smtpAddress = details[2];

Working with scriptable objects

If you want to interact with scriptable objects such as the File System Object, you just need to create it as you would normally within a standalone script. The code below will create a File System Object, then use it to check if a file exists:

var sharedFso = new ActiveXObject("Scripting.FileSystemObject");
if (sharedFso.FileExists("DeleteMe.txt"))
{
    sharedFso.DeleteFile("DeleteMe.txt", true);
}

Interacting with the user

Your script can interact with the user by calling back into the installer session. Here is an example of displaying a Yes / No message box:

var msiMessageTypeUser     = 0x03000000;
var msiMessageTypeYesNo    = 4;
var msiMessageTypeDefault1 = 0x000;

var options =    msiMessageTypeUser
        + msiMessageTypeYesNo
        + msiMessageTypeDefault1;

var objRecord = Session.Installer.CreateRecord(1);
objRecord.StringData(0) = "[1]";
objRecord.StringData(1) = "Do you want to create the file?";

var response = Session.Message(options, objRecord);

Conclusion

Hopefully, this quick article has given you some useful pointers on how to use scripts from an installer created using a Visual Studio Deployment project. Deployment projects are fairly limited in the functionality they provide. However, the tool is free with Visual Studio, and with the ability to call an arbitrary script, they can be quite powerful.

The download includes a project which illustrates all of the above features.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Piers Lawson
Architect
United Kingdom United Kingdom
Piers is currently working for Distribution-Technology, one of the UK's fastest growing private companies.
 
He has worked in the military, broadcast, gaming and financial industries. This has been for a mixture of large corporations (such as GEC, Sony and CSC) and smaller companies (e.g. Kismet Gaming).

Comments and Discussions

 
GeneralDo not proceed on the installation for certain conditions Pinmemberchunsh22-Apr-09 10:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 4 Oct 2006
Article Copyright 2006 by Piers Lawson
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid