Data Exchange with Infragistics XamGantt Control - Import/Export to Microsoft Project (Part 2)

[Infragistics] Mihail Mateev / Tuesday, October 16, 2012

Infragistics XamGantt Control works with a Project instance that contains the task, resource and calendar information for the schedule and has a built in scheduling engine supporting many of the features present in Microsoft Project 2010.

For developers using Infragistics Gantt control will be a challenge to offer an option for data exchange with the established product on the market like Microsoft Project that is the most popular product for project management.

In the previous blog from this series, "Data Exchange with Infragistics XamGantt Control - Using XML (Part 1)" we discussed about how to serialize / deserialize models used for Gantt projects in its own XML format.

In this article we will learn how to collaborate with  Microsoft Project .

There are many different approaches to work with MS Project data:
- Using data exchange through files (XML or MS Project format (*. mpp))
- Interact with a Microsoft Project COM-based object model via Office Primary Interop Assemblies (PIA)

We will follow the easiest way for data exchanged based on XamGantt and MS Project features.

When you want your system to exchange data with Microsoft Project you will probably try to exchange data files in MS Project. Infragistics Gantt Control can read projects in MS Project XML format, but for now it does not export data in this format.
Possible solution to export is to have own implementation of the export to MS Project format using the Project XML Data Interchange Schema Reference and mspdi_pj14.xsd schema included in the Project 2010 SDK download.

Let’s create a simple WPF application and add XamGantt control instance inside it. You can start with the samples from my previous post: “How to Start Using Infragistics XamGantt Control” or "Data Exchange with Infragistics XamGantt Control - Using XML (Part 1)". Add buttons to call import from and export to project.

What you need in advance:

Import data from Microsoft Project

Infragistics XamGantt control supports out of the box import from Microsoft Project via XML format. It is pretty easy to import projects that you created in MS Project in your application where is used Gantt control.

Let’s create create a new project with Microsoft Project 2010 (you can use also MS Project 2007) and let's add a few tasks for this project.

  

Export project data to XML file

You can open XML file in your application as a stream and pass this stream to Project.LoadFromProjectXml method.

Code below shows how to open your XML file.

   1: #region btnImport_Click
   2: private void btnImport_Click(object sender, RoutedEventArgs e)
   3: {
   4:     OpenFileDialog dialog = new OpenFileDialog();
   5:     dialog.Multiselect = false;
   6:     dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
   7:  
   8:     dialog.Filter = "XML Files(*.xml)|*.xml";
   9:     dialog.Title = "Select XML File";
  10:  
  11:     bool? isOpened = dialog.ShowDialog();
  12:     if (isOpened == true)
  13:     {
  14:         try
  15:         {
  16:             using (Stream stream = dialog.OpenFile())
  17:             {
  18:                 LoadProjectFromStream(stream);
  19:  
  20:                 stream.Close();
  21:             }
  22:         }
  23:         catch (Exception ex)
  24:         {
  25:             MessageBox.Show("Error  " + ex.Message);
  26:         }
  27:     }
  28:  
  29: }
  30: #endregion btnImport_Click

 

The code below demonstrates how to use Project.LoadFromProjectXml(Stream) method to create a project in XamGantt control from XML file, exported from MS Project.

   1: #region LoadProjectFromStream
   2: private void LoadProjectFromStream(Stream stream)
   3: {
   4:     // Create a xamGantt Project and load project data from a Stream
   5:     var model = this.Root.Resources["nativeviewmodel"] as NativeProjectViewModel;
   6:     if (model != null)
   7:     {
   8:         var project = new Project();
   9:         project.LoadFromProjectXml(stream);
  10:         this.nativeGantt.VisibleDateRange = new DateRange(project.Start, project.Finish);
  11:         model.Project = project;
  12:     }
  13: }
  14: #endregion LoadProjectFromStream

 

You can use standard OpenFileDialog to select the XML file.

The imported in XamlGantt application project seems to have all the features. You can continue to handle data in your application.

 

Export data to Microsoft Project

Exporting data from XamGantt control to Microsoft Project has more details and is not supported out of the box from Infragistics Gantt component.

An natural choice would be to export data in XML, which is supported by Microsoft Project for data exchange.
The MS Project XML structure is is very complicated. You need to add data, that is not required when you work with XamGantt control.  You can create own implementation of the export to MS Project format using the Project XML Data Interchange Schema Reference and mspdi_pj14.xsd schema included in the Project 2010 SDK download.

This approach will take a lot of time. If you need to implement data export faster you can use Microsoft Project COM-based object model. To use the features of a Microsoft Office application from an Office project, you must use the primary interop assembly (PIA) for the application. The PIA enables managed code to interact with a Microsoft Office application's COM-based object model.

When you install Visual Studio, the PIAs are automatically installed to a location in the file system, outside of the global assembly cache. When you create a new project, Visual Studio automatically adds references to these copies of the PIAs to your project. Visual Studio uses these copies of the PIAs, instead of the assemblies in the global assembly cache, to resolve type references when you develop and build your project.

Visual Studio installs these copies of PIAs to the following locations on the development computer:

  • %ProgramFiles%\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office12
    (or %ProgramFiles(x86)%\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office12 on 64-bit operating systems)

  • %ProgramFiles%\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14
    (or %ProgramFiles(x86)%\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14 on 64-bit operating systems)

If you have no installed PIA assemblies you could download MS Office 2010 PIA here:

In this sample You will learn how to export data using Office PIA. Unlike the other examples here will demonstrate conjunction with MS Project 2007. Implementation of MS Project 2010 is analogous. The only difference is in referenced assemblies.

You need to have a project with tasks in your XamGantt application.

 

You need to add reference to Microsoft.Office.Interop.MSProject assembly in your project.

   1: #region btnExport_Click
   2: private void btnExport_Click(object sender, RoutedEventArgs e)
   3: {
   4:     var tabIndex = this.ganttTabControl.SelectedIndex;
   5:  
   6:     if (tabIndex == 0)
   7:     {
   8:         var model = this.Root.Resources["viewmodel"] as ProjectViewModel;
   9:  
  10:         if (model != null)
  11:         {
  12:             Project project = xamGantt.Project;
  13:             GenerateExportProject(project);
  14:         }
  15:     }
  16:  
  17:     else if (tabIndex == 1)
  18:     {
  19:         var model = this.Root.Resources["nativeviewmodel"] as NativeProjectViewModel;
  20:         if (model != null)
  21:         {
  22:             Project project = model.Project;
  23:             GenerateExportProject(project);
  24:         }
  25:     }
  26:  
  27: }
  28: #endregion btnExport_Click

You can use Interop.MSProject with C# or VB.Net to create an MS Project instance, get the active project and in a loop to create MS Project tasks and pass to these objects data from your XamGantt.ProjectTask instances.

   1: private void GenerateExportProject(Project ganttProject)
   2: {
   3:     //Run MS Project
   4:     var projectApp = new msproject.Application();
   5:     projectApp.AppMaximize();
   6:     projectApp.FileNew(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
   7:  
   8:     msproject.Project project = projectApp.ActiveProject;  
   9:  
  10:     foreach (ProjectTask projectTask in ganttProject.RootTask.Tasks)
  11:     {
  12:         msproject.Task msTask = null;
  13:         var taskName = projectTask.TaskName;
  14:         if (taskName != null)
  15:         {
  16:             taskName = taskName.Replace(' ', '_');
  17:             msTask = project.Tasks.Add(Name = taskName);
  18:         }
  19:  
  20:         if (projectTask.Start != null)
  21:         {
  22:             msTask.Start = projectTask.Start;
  23:         }
  24:  
  25:  
  26:         if (projectTask.Finish != null)
  27:         {
  28:             msTask.Finish = projectTask.Finish;
  29:         }
  30:         
  31:  
  32:         msTask.Predecessors = projectTask.PredecessorsIdText;
  33:  
  34:     }
  35: }
  36: #endregion GenerateExportProject

 

That's it - now you have your data in Microsoft Project! You can implement this very fast and easy – just several lines of code!

 

Conclusions

It is very easy to exchange data between Gantt projects and MS Project if you know the appropriate approach

  • For import use built-in Project.LoadFromProjectXml method
  • For export use Microsoft Project COM-based object model via Office Primary Interop Assemblies (PIA)

Follow news from Infragistics for more information about new Infragistics products.

Source code is available here. Sample project is created with Visual Studio 2012, but you can use NetAdvantage 12.2 controls with both: Visual Studio 2012 and Visual Studio 2012. As always, you can follow us on Twitter @mihailmateev and @Infragistics and stay in touch on Facebook, Google+ and LinkedIn!