3

I'm having a heckuva time finding any C# code examples using the MPXJ library when connecting to a Microsoft Project file. Can someone please post a snippet demonstrating how to write the contents of a table in an .mpp file to screen?

Bonus points for any links/references!

Thanks!

~Dan

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
Daniel Szabo
  • 7,181
  • 6
  • 48
  • 65

1 Answers1

5

hopefully this will help.

First you need to open your project file:

ProjectReader reader = ProjectReaderUtility.getProjectReader(inputFile);
ProjectFile projectFile = reader.read(inputFile);

This assumes that you have a file name in the inputFile string.

The method below should be treated as pseudocode (i.e. I haven't compiled it, shaken the bugs out of it and so on, and it's not the most elegant thing I've ever written), but it illustrates the approach:

public void dumpTables(ProjectFile file)
{
    List tables = file.getTables();
    Iterator iter = tables.iterator();
    while (iter.hasNext())
    {
        Table table = (Table)iter.next();
        if (table.getResourceFlag())
        {
            List resources = file.getAllResources();
            Iterator resourceIter = resources.iterator();
            while (resourceIter.hasNext())
            {
                Resource resource = (Resource)iter.next();
                List columns = table.getColumns();
                Iterator columnIter = columns.iterator();
                while (columnIter.hasNext())
                {
                    Column column = (Column)columnIter.next();
                    Object columnValue = resource.getCachedValue(column.getFieldType());
                    Console.Write(columnValue);
                    Console.Write(",");
                }
                Console.WriteLine();
            }
        }
        else
        {
            List tasks = file.getAllTasks();
            // etc. as above
        }
    }
}

The idea is that you are retrieving the list of tables present in the file, and for each one working out if it is a Task or Resource table. Based on this you'll grab the list of tasks or resources, iterate through that, and for each instance pull out the column value and display it. Note that I've not made any attempt to order the tasks or resources in any particular way. I'll leave that as an exercise for the reader!

Hope that helps!

Jon

Jon Iles
  • 2,519
  • 1
  • 20
  • 30
  • Thanks, Jon! I really appreciate the personal attention you give to the users of the product you maintain. Your continued commitment the community is VERY much appreciated! – Daniel Szabo Dec 16 '10 at 20:35
  • quick suggestion -- you might want to edit your response to show future viewers what includes they'll need at the top of the CS file (using net.sf.mpxj etc etc ). Thanks, again! – Daniel Szabo Dec 16 '10 at 21:21
  • @Jon - Its been 12 years. Are there any updated samples (in C#) for this? – Siddhant Dec 07 '22 at 13:21
  • A few! This is the repo of interest: https://github.com/joniles/mpxj-dotnet-samples, this folder in particular: https://github.com/joniles/mpxj-dotnet-samples/tree/main/netcoreapp3.1/MpxjSamples – Jon Iles Dec 08 '22 at 14:16