How To: Use MPXJ with .Net

MPXJ ships with both a Java library (mpxj.jar) and also a .Net DLL (mpxj.dll), which can be found in the folder of the distribution. To use MPXJ from a .Net language, simply ensure that the DLLs present in the folder are all added as references to your project.

MPXJ ships with some sample files which can be found in the folder of the distribution. These files illustrate how the MPXJ API can be used to manipulate project data. In particular the MpxjQuery sample files show how various elements which make up the project data can be queried. Two versions of this utility are present in, one written in C#, and the other written in Visual Basic (VB) to illustrate the basics of using MPXJ in either language. Even if you are developing software in a .Net language you may still find it useful to refer to the Java examples, and indeed the original Java source of MPXJ to give you an insight into how the API may be used.

The .Net version of MPXJ has been generated directly from the Java version using a tool called IKVM. One of the side effects of using IKVM to perform this conversion is that the MPXJ exposes .Net versions of the original Java data types, so for example you will find that the API returns a type called Date rather than a .Net DateTime, and collections which don't expose the familiar IEnumerable interface.

To try and make the use of MPXJ as natural as possible for .Net developers, two specific areas have been addressed. The first area is the MPXJ API itself: in the original Java version of the code you will find that properties use accessor and modifier methods like those shown below:

String text = task.getText();

In the .Net version of the API, in most cases you will find that .Net properties are available, so the Java style methods do not need to be used:

String text = task.Text
task.Text = text;

The other area of assistance provided to .Net developers is a simple set of extension methods. In the Visual Studio project found in the folder, a project called MpxjExtensionMethods is present. This project contains extension methods which enhance both Java and .Net classes to make it easier to pass data to and from the API. For example the extension method ToIEnumerable is added to Java collection data types which allows them to be iterated using the familiar foreach .Net syntax.

To use these extension methods, simply add a reference to the MpxjExtensionMethods assembly in your own project. The methods themselves are documented in the source, and examples of their use can be seen in the samples provided in the folder.