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
 2Microsoft.Office.Interop.MSProject.Application projectApplication = new MSProject.Application();
 3object missingValue = System.Reflection.Missing.Value;
 4//Open an MPP file
 5projectApplication.FileOpenEx("Project1.mpp",
 6    missingValue, missingValue, missingValue, missingValue,
 7    missingValue, missingValue, missingValue, missingValue,
 8    missingValue, missingValue, PjPoolOpen.pjPoolReadOnly,
 9    missingValue, missingValue, missingValue, missingValue,
10    missingValue);
11Microsoft.Office.Interop.MSProject.Project project = projectApplication.ActiveProject;
12int iResourceId = 1;
13foreach (Task tsk in project.Tasks)
14{
15    string developer = "Developer0" + iResourceId;
16    project.Resources.Add(developer, iResourceId);
17    tsk.Assignments.Add(tsk.ID, iResourceId, missingValue);
18    iResourceId++;
19}
20projectApplication.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// Declare ChildTasksCollector class object
 3ChildTasksCollector collector = new ChildTasksCollector();
 4// Use TaskUtils to get all children tasks in RootTask
 5TaskUtils.Apply(prj.RootTask, collector, 0);
 6// Define Resources
 7ArrayList resources = new ArrayList();
 8for (int i = 1; i <= 5; i++)
 9{
10    string developer = "Developer0" + i;
11    // Create resource
12    Resource rec = new Resource(developer);
13    rec.Type = ResourceType.Work;
14    // Add resource to project
15    prj.Resources.Add(rec);
16    // define assignment
17    ResourceAssignment assignment = new ResourceAssignment((Aspose.Tasks.Task)collector.Tasks[i], rec);
18    prj.ResourceAssignments.Add(assignment);
19}
20prj.CalcResourceUids();
21prj.CalcResourceIds();
22prj.CalcResourceFields();
23prj.CalcResourceAssignmentUids();
24prj.CalcResourceAssignmentIds();
25prj.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.