Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Accessing MPP File using MPXJ Lib using C#

0.00/5 (No votes)
3 Mar 2014 1  
How to access MPP file using MPXJ Lib using C#

Introduction

If the data resides in Excel, then you have a lot of options to extract the data irrespective of technology. But if the same requirement comes for a MPP (Microsoft Project Plan) file, you have a limited number of options.

MPXJ is an open source library that is used to retrieve data from MPP file. This library provides a set of facilities to allow project information to be manipulated in Java and .NET.

MPXJ supports a range of data formats:

  • Microsoft Project (MPP, MPT)
  • Microsoft Project Exchange (MPX)
  • XML
  • MPD
  • And various others

MPXJ is based around a "neutral" data structure which is used to represent project data, coupled with a set of format-specific reader and writer classes which understand how to read from and write to the various supported file formats. The library is currently based around a set of structures modeled on the data described by the MPP file format. All manipulation of project data takes place using these data structures, which can be read from or written to the various supported file formats.

The figure below illustrates the key entities represented by the MPXJ data structure.

Fig 1.0

MPXJ currently allows project data to be read from eight distinct data sources using the following reader classes:

  • net.sf.mpxj.mpp.MPPReader: reads Microsoft Project MPP files
  • net.sf.mpxj.mpx.MPXReader: reads Microsoft MPX files
  • net.sf.mpxj.mspdi.MSPDIReader: reads Microsoft MSPDI (XML) files
  • net.sf.mpxj.mpd.MPDReader: reads Microsoft MPD files
  • net.sf.mpxj.planner.PlannerReader: reads Planner (XML) files
  • net.sf.mpxj.primavera.PrimaveraDatabaseReader: reads from a Primavera database
  • net.sf.mpxj.primavera.PrimaveraPMFileReader: reads Primavera PM XML files
  • net.sf.mpxj.primavera.PrimaveraXERFileReader: reads Primavera XER files

A similar arrangement exists for the writer classes:

  • net.sf.mpxj.mpx.MPXWriter: writes Microsoft MPX files
  • net.sf.mpxj.mspdi.MSPDIWriter: writes Microsoft MSPDI (XML) files
  • net.sf.mpxj.planner.PlannerWriter: writes Planner (XML) files

MPXJ is written and maintained in Java, however this is no barrier to using its functionality in .NET. IKVM.NET allows MPXJ to be used from any .NET programming language without having to be aware that the original code was written in Java. IKVM.NET is an implementation of Java for Mono and the Microsoft .NET Framework. It includes the following components:

  • A Java virtual machine implemented in .NET
  • A .NET implementation of the Java class libraries
  • Tools that enable Java and .NET interoperability

In order to use this library, you can use the following steps:

  • Add Reference of MPXJ.dll and all necessary DLLs (provided DLLs)
  • We need to create class EnumerableCollection.cs to wrap IKVM collection to allow enumeration:

public class EnumerableCollection
{
    public EnumerableCollection(Collection collection)
    {
       m_collection = collection;
    }

    public IEnumerator GetEnumerator()
    {
        return new Enumerator(m_collection);
    }

    private Collection m_collection;
}

public class Enumerator : IEnumerator
{
    public Enumerator(Collection collection)
    {
        m_collection = collection;
        m_iterator = m_collection.iterator();
    }

    public object Current
    {
        get
        {
            return m_iterator.next();
        }
    }

    public bool MoveNext()
    {
        return m_iterator.hasNext();
    }

    public void Reset()
    {
        m_iterator = m_collection.iterator();
    }

    private Collection m_collection;
    private Iterator m_iterator;
}

  1. Import the following namespaces:
    using net.sf.mpxj;
    using net.sf.mpxj.reader;
    using net.sf.mpxj.writer;
    using net.sf.mpxj.mpp;
  2. Create object of ‘ProjectReader’ and ‘ProjectFile’ class
    ProjectReader reader = new MPPReader(); 
    ProjectFile projectObj = reader.read(<FileName>);
  3. For easy enumeration in collection, we can create a method ‘ToEnumerable
    private static EnumerableCollection ToEnumerable(Collection javaCollection)
            {
                return new EnumerableCollection(javaCollection);
              }
  4. Now if we want to access tasks, subtasks, calendar, resources from MPP file, then we need to use methods using ProjectFile Object
    1. Tasks
      foreach (Task task in ToEnumerable(projectObj.getAllTasks()))
          {
      Console.WriteLine("Task: " + task.getName() + " ID=" + task.getID() + " Unique ID=" + task.getUniqueID());
          }
    2. Resources
      foreach (Resource resource in ToEnumerable(projectObj.getAllResources())
          {
      Console.WriteLine("Resource: " + resource.getName() + "(Unique ID=" + resource.getUniqueID() + ")");
          }
    3. SubTasks
      foreach (Task child in ToEnumerable(task.getChildTasks()))
              {
                    Console.WriteLine(indent + "Task: " + child.getName());
                    listHierarchy(child, indent + " ");
             }
    4. Access Resource and Task by ID
      • Resource r = project.getResourceByUniqueID(Integer.valueOf(99));
      • Task t = project.getTaskByUniqueID(Integer.valueOf(99));
    5. Calendar
      ProjectCalendar defaultCalendar = projectObj.getCalendar();
      ProjectCalendar taskCalendar = task.getCalendar();
  5. There are many methods provided by MPXJ library you can use according to your requirements.
  6. Now if you want to create a MPP file, use the following steps:
    1. Create Source ProjectFile Object
      • ProjectReader reader = new MPPReader();
      • ProjectFile sourceObj = reader.read(<sourcefilename>);
    2. Create object of ’ProjectWriter’ class
      ProjectWriter writer = ProjectWriterUtility.getProjectWriter(<outputFile Name>);
      writer.write(sourceObj, <outputFile Name>);

Reference(s)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here