Thursday, July 24, 2008

Open web pages from X++ code or Run External Application

Run External Application

The interesting part is that executing external application is fairly effortless in Dynamics Ax. X++ is capable of calling Microsoft Windows Application Programming Interface (API). The common functionalities of the Win API have been built in classes WinAPI, WinAPIServer, WinGDI and WinInet. Running external application could be achieved through the static method WinAPI::shellExecute.

Static Method WinAPI::shellExecute

This method takes six parameters where five of them are optional parameters. The following code segment shows the interface of this method.

client static int shellExecute(
Filename _lpFile,
str _lpParameters = '',
str _lpDirectory = '',
str _lpOperation = #ShellExeOpen,
int _show = #SW_SHOWNORMAL,
boolean _waitForCompletion = false
)

The interface might look complicated but the first parameter is usually all we need to assign. It is sufficient to achieve most of the scenarios. The second parameter allows us to execute an executable with parameters. We will look at examples where this second parameter comes into play later.

Class SysShellExecute

The class SysShellExecute facilitates calling WinAPI::shellExecute. This class has a main method that call the method WinAPI::shellExecute using args.parm() as the first parameter. This enables WinAPI::shellExecute to be called from menu item with ease. This is important Dynamics Ax bring up windows through menu item. Menu item works with buttons with ease.

The previous approach opens the file or URL in the default application. There are cases where you need to specify the application to open the file with. You may achieve this with static method WinAPI::shellExecute.

Let say the default browser for your computer is FireFox and the website you are opening requires Internet Explorer. You may use the following code to open the URL with Internet Explorer.

WinApi::shellExecute("http://www.yahoo.com");

Using Info Class

use urlLookup() method of infoclass
If you want to open the Internet Explorer with a certain web page directly from X++ code, you can use the following statement:

infoLog.urlLookup('http://www.yahoo.com');

Thus, your browser will open and display the URL.

No comments: