Create Resources and Link in VSTO and Aspose.Tasks
Code Examples
VSTO
The following steps are required to accomplish this task:
Create a new project in Visual Studio.
In the Solution Explorer, right-click and select Add Reference, then select the COM components tab.
Select Microsoft Project 12.0 Object Library and click OK. This imports the Microsoft.Office.Interop.MSProject namespace at the start of the code.Use the code from the following example to read tasks and resources.
1//Create an Application object
2
3Microsoft.Office.Interop.MSProject.Application projectApplication = new MSProject.Application();
4
5object missingValue = System.Reflection.Missing.Value;
6
7//Open an MPP file
8
9projectApplication.FileOpenEx("Project1.mpp",
10
11 missingValue, missingValue, missingValue, missingValue,
12
13 missingValue, missingValue, missingValue, missingValue,
14
15 missingValue, missingValue, PjPoolOpen.pjPoolReadOnly,
16
17 missingValue, missingValue, missingValue, missingValue,
18
19 missingValue);
20
21Microsoft.Office.Interop.MSProject.Project project = projectApplication.ActiveProject;
22
23int iResourceId = 1;
24
25foreach (Task tsk in project.Tasks)
26
27{
28
29 string developer = "Developer0" + iResourceId;
30
31 project.Resources.Add(developer, iResourceId);
32
33 tsk.Assignments.Add(tsk.ID, iResourceId, missingValue);
34
35 iResourceId++;
36
37}
38
39projectApplication.FileCloseAll(Microsoft.Office.Interop.MSProject.PjSaveType.pjSave);
This C# code uses the Microsoft Project Interop API to open an existing Microsoft Project file (Project1.mpp), programmatically add resources, and assign those resources to tasks. Also this example demonstrates how to dynamically generate and assign resources to tasks in an .mpp file using Interop, simulating a scenario where each task receives a dedicated developer. This approach requires Microsoft Project to be installed on the system.
Aspose.Tasks
The following steps are required to accomplish this task:
Create a new project in Visual Studio.
In the Solution Explorer, right-click and select Add Reference, then select the .NET tab.
Select Aspose.Tasks and then click OK. This imports the Aspose.Tasks namespace at the start of the code.Use the code from the following example to create resources and link them to tasks.
1Project prj = new Project("Project.mpp");
2
3// Declare ChildTasksCollector class object
4
5ChildTasksCollector collector = new ChildTasksCollector();
6
7// Use TaskUtils to get all children tasks in RootTask
8
9TaskUtils.Apply(prj.RootTask, collector, 0);
10
11// Define Resources
12
13ArrayList resources = new ArrayList();
14
15for (int i = 1; i <= 5; i++)
16
17{
18
19 string developer = "Developer0" + i;
20
21 // Create resource
22
23 Resource rec = new Resource(developer);
24
25 rec.Type = ResourceType.Work;
26
27 // Add resource to project
28
29 prj.Resources.Add(rec);
30
31 // define assignment
32
33 ResourceAssignment assignment = new ResourceAssignment((Aspose.Tasks.Task)collector.Tasks[i], rec);
34
35 prj.ResourceAssignments.Add(assignment);
36
37}
38
39prj.CalcResourceUids();
40
41prj.CalcResourceIds();
42
43prj.CalcResourceFields();
44
45prj.CalcResourceAssignmentUids();
46
47prj.CalcResourceAssignmentIds();
48
49prj.Save("Project1_CSharp_Aspose.mpp", Aspose.Tasks.Saving.SaveFileFormat.MPP);
This C# code demonstrates how to use Aspose.Tasks for .NET to programmatically create resources and assign them to tasks in a Microsoft Project (.mpp
) file. It begins by loading an existing project and using TaskUtils.Apply
to collect all child tasks from the root task into a ChildTasksCollector
object. It then creates a list of five work resources, named “Developer01” through “Developer05”, and adds each one to the project. For each resource, it creates a corresponding ResourceAssignment
, linking it to a specific task from the collected list. After all assignments are added, several calculation methods are called to ensure internal consistency of resource IDs and UIDs within the project structure. Finally, the updated project is saved as a new .mpp
file.