Create Resources and Link with Tasks
Contents
[
Hide
Show
]While working with MPP files, you might need to create resources and link them to tasks from your project. This article gives an idea about how to load MPP files in your .NET applications and create resources and link to tasks from your projects using VSTO and Aspose.Tasks for .NET.
Create Resources and Link them to Tasks using 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.
The sample project file used in the following code snippets, SampleProject.mpp, can be downloaded from this page attachments. Before resources are created and linked to tasks, the Project1.mpp file looks as below.
Input file: Gantt chart view
Input file: Resource sheet view
After processing, the file is updated.
Output file: Gantt chart view
Output file: Resource sheet view
1Microsoft.Office.Interop.MSProject.Application projectApplication = new Application();
2object missingValue = System.Reflection.Missing.Value;
3projectApplication.FileOpenEx(@"SampleProject.mpp",
4 missingValue, missingValue, missingValue, missingValue,
5 missingValue, missingValue, missingValue, missingValue,
6 missingValue, missingValue, PjPoolOpen.pjPoolReadOnly,
7 missingValue, missingValue, missingValue, missingValue,
8 missingValue);
9Microsoft.Office.Interop.MSProject.Project project = projectApplication.ActiveProject;
10int iResourceId = 1;
11foreach (Task tsk in project.Tasks)
12{
13 string developer = "Developer0" + iResourceId;
14 project.Resources.Add(developer, iResourceId);
15 tsk.Assignments.Add(tsk.ID, iResourceId, missingValue);
16 iResourceId++;
17}
18projectApplication.FileCloseAll(Microsoft.Office.Interop.MSProject.PjSaveType.pjSave);
Create Resources and Link to Tasks using Aspose.Tasks for .NET
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 project = new Project("New Project.mpp");
2
3// Declare ChildTasksCollector class object
4ChildTasksCollector collector = new ChildTasksCollector();
5
6// Use TaskUtils to get all children tasks in RootTask
7TaskUtils.Apply(project.RootTask, collector, 0);
8
9// Define Resources
10for (int i = 0; i <= 4; i++)
11{
12 // Add resource to project
13 Resource newResource = project.Resources.Add("Developer0" + i);
14 newResource.Set(Rsc.Type, ResourceType.Work);
15
16 // Define assignment
17 ResourceAssignment newResourceAssignment = project.ResourceAssignments.Add((Task)collector.Tasks[i], newResource);
18}
19
20project.Save("CreateResourcesAndLinkToTasks_out.mpp", SaveFileFormat.MPP);