Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

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

4.20/5 (5 votes)
3 Oct 2006CPOL3 min read 1   550  
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:

JavaScript
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:

JavaScript
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:

JavaScript
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)