Click here to Skip to main content
15,867,895 members
Articles / Programming Languages / C#
Tip/Trick

Accessing MPP File using MPXJ Lib using C#

Rate me:
Please Sign up or sign in to vote.
4.96/5 (9 votes)
3 Mar 2014CPOL3 min read 34.5K   92   4   5
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.

Image 1

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:

C#
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:
    C#
    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
    C#
    ProjectReader reader = new MPPReader(); 
    ProjectFile projectObj = reader.read(<FileName>);
  3. For easy enumeration in collection, we can create a method ‘ToEnumerable
    C#
    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
      C#
      foreach (Task task in ToEnumerable(projectObj.getAllTasks()))
          {
      Console.WriteLine("Task: " + task.getName() + " ID=" + task.getID() + " Unique ID=" + task.getUniqueID());
          }
    2. Resources
      C#
      foreach (Resource resource in ToEnumerable(projectObj.getAllResources())
          {
      Console.WriteLine("Resource: " + resource.getName() + "(Unique ID=" + resource.getUniqueID() + ")");
          }
    3. SubTasks
      C#
      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
      C#
      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
      C#
      ProjectWriter writer = ProjectWriterUtility.getProjectWriter(<outputFile Name>);
      writer.write(sourceObj, <outputFile Name>);

Reference(s)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
I like to code and I really enjoy to share my knowledge with all, Its my passion.

http://abhishekgoswami.com/

Comments and Discussions

 
QuestionMPP viewer Pin
Member 1179733417-Feb-18 10:09
Member 1179733417-Feb-18 10:09 
Questionthe zip in the browse code section only contains a doc file Pin
nicetohaveyou5-May-16 13:51
nicetohaveyou5-May-16 13:51 
PraiseAmazing tutorial Pin
Member 1242737230-Mar-16 22:24
Member 1242737230-Mar-16 22:24 
GeneralRe: Amazing tutorial Pin
Abhishek Kumar Goswami31-Mar-16 5:28
professionalAbhishek Kumar Goswami31-Mar-16 5:28 
GeneralMy vote of 5 Pin
blachsmith17-Apr-14 19:50
blachsmith17-Apr-14 19:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.