Reading Tasks and Resources

While working with MPP files, you might need to read tasks and resources from your project. This article gives a small idea about how to load MPP files in your .NET applications and read tasks or resources from your projects using VSTO and Aspose.Tasks for .NET.

Read Tasks and Resources Using VSTO

The following steps are required to accomplish this task:

  1. Create a new project in Visual Studio.
  2. In the Solution Explorer, right-click and select Add Reference, then select the COM components tab.
  3. Select Microsoft Project 12.0 Object Library and click OK. This imports the Microsoft.Office.Interop.MSProject namespace at the start of the code.
  4. Use the code from the following example to read tasks and resources.
 1// Create Application object
 2Application projectApplication = new ApplicationClass();
 3object missingValue = System.Reflection.Missing.Value;
 4projectApplication.FileOpenEx(@"C:\Project1.mpp",
 5    missingValue, missingValue, missingValue, missingValue,
 6    missingValue, missingValue, missingValue, missingValue,
 7    missingValue, missingValue, PjPoolOpen.pjPoolReadOnly,
 8    missingValue, missingValue, missingValue, missingValue,
 9    missingValue);
10Project project = projectApplication.ActiveProject;
11foreach (Task task in project.Tasks)
12{
13    Console.WriteLine("Reading Task " + task.Name);
14    Console.WriteLine("\nID: " + task.ID);
15    Console.WriteLine("Start: " + task.Start);
16    Console.WriteLine("Finish: " + task.Finish);
17    Console.WriteLine("\n===========================\n");
18}
19// Loop through each resource and read information related to resources
20foreach (Resource resource in project.Resources)
21{
22    string resourceType = null;
23    switch (resource.Type)
24    {
25        case PjResourceTypes.pjResourceTypeCost:
26            resourceType = "Cost";
27            break;
28        case PjResourceTypes.pjResourceTypeMaterial:
29            resourceType = "Material";
30            break;
31        case PjResourceTypes.pjResourceTypeWork:
32            resourceType = "Work";
33            break;
34    }
35    Console.WriteLine("Reading Resource " + resource.Name);
36    Console.WriteLine("\nID: " + resource.ID);
37    Console.WriteLine("Type: " + resourceType);
38    Console.WriteLine("\n===========================\n");
39}
40Console.ReadLine();

Read Tasks and Resources Using Aspose.Tasks for .NET

The following steps are required to accomplish this task:

  1. Create a new project in Visual Studio.
  2. In the Solution Explorer, right-click and select Add Reference, then select the .NET tab.
  3. Select Aspose.Tasks and then click OK. This imports the Aspose.Tasks namespace at the start of the code.
  4. Use the code from the following example to read tasks and resources.
 1Project project = new Project("New Project.mpp");
 2
 3// Load all tasks
 4TaskCollection allTasks = project.RootTask.Children;
 5
 6// Loop through each task and read information related to tasks
 7foreach (Task task in allTasks)
 8{
 9    Console.WriteLine("Reading Task " + task.Get(Tsk.Name));
10    Console.WriteLine("ID: " + task.Get(Tsk.Id));
11    Console.WriteLine("Start: " + task.Get(Tsk.Start));
12    Console.WriteLine("Finish: " + task.Get(Tsk.Finish));
13}
14
15// Loop through each resource and read information related to resources
16foreach (Resource resource in project.Resources)
17{
18    string resourceType = null;
19    switch (resource.Get(Rsc.Type))
20    {
21        case ResourceType.Material:
22            resourceType = "Material";
23            break;
24        case ResourceType.Work:
25            resourceType = "Work";
26            break;
27        default:
28            resourceType = "Cost";
29            break;
30    }
31
32    Console.WriteLine("Reading Resource " + resource.Get(Rsc.Name));
33    Console.WriteLine("ID: " + resource.Get(Rsc.Id));
34    Console.WriteLine("Type: " + resourceType);
35}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.