Monday, February 18, 2008

Calling an application's window in a Web Form application

One question I'm frequently asked is, "How can I have my PowerBuilder application's windows turned into separate web pages?". If you've deployed a PowerBuilder application to the web using the .NET Web Form target, and then examined the application's directory on the web server you've noticed that there is only one ASP .NET page created (default.aspx)!

Think of that as the web equivalent of the bootstrapper .exe PowerBuilder generates in a traditional Windows application. The goal for PowerBuilder 11 was to allow you to take existing Windows applications to the web with a minimal amount of refactoring. That's all well and good, but many of you out there want to incorporate pieces of your existing PowerBuilder applications into standalone web applications or portals created using Visual Studio or some other product. Don't worry, all is not lost. It might take a little extra work (how much work depends on how you've designed your client server application in the first place) but you can leverage your existing client server application logic into new web applications without having to do a complete rewrite.

The trick is to use the commandline parameter. If you open the application object in PowerBuilder and go to the Open event, you'll notice that it recieves one argument called Commandline:




For Web Form targets, PowerBuilder lets you pass a value to the commandline by using the PBCommandParm parameter. It is as straightforward as calling the application in a URL like this:

http://myserver/myapp/default.htm?PBCommandParm=CUSTOMER

In the application Open event you can capture the value and process it any way you like. For example:

CHOOSE CASE commandline
CASE "CUSTOMER"
Open (w_mss_customer_master)
CASE "PRODUCT"
Open (w_mss_product_master)
CASE ELSE
Open ( w_mssmain_frame )
END CHOOSE

Using the Marketing Support System sample application as an example, if the application were to be run with no commandline parameter, the standard MDI frame would open and the browser would looks something like this:

http://myserver/myapp



If we call the same application but use a URL like this:

http://myserver/myapp/default.htm?PBCommandParm=CUSTOMER

The browser will open and select the Customer window:





Note: we call the default.htm file to hide the web browser's menu and toolbar. Default.htm is just a wrapper to the default.aspx but it contains some Javascript code to resize the browser and hide the menu and toolbar.

So that's all there is to it. Or is it? What if you want to pass multiple parameters? Well there is only one commandline parameter for the application's open event, but you can pass multiple parameters in using %20, the ASCII character code for an empty space in the URL.

http://myserver/myapp/default.htm?PBCommandParm=CUSTOMER%20FISH

All of the parameters will get concatenated into one commandline string so you will need to use the left, mid, or right functions, along with the pos function in Powerscript to parse them out (see the PowerBuilder Help file for details).