Create Resources and Link in VSTO and Aspose.Tasks

Code Examples

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.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);

Aspose.Tasks

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.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);

Download Sample Code

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.