Creating Resource Assignments

Resource assignments are links between a task and a resource. This article explains how to create resource assignment using Aspose.Tasks for .NET.

Creating Resource Assignments

The Resource class exposes two different ways of creating a resource assignment, either using the default constructor or by passing a task and resource.

Programming Sample: Default Constructor Create a ResourceAssignment instance without requiring any parameters to be passed.

1// Create empty project
2Project project = new Project();
3
4// Add new task and resource
5Task task = project.RootTask.Children.Add("Task");
6Resource resource = project.Resources.Add("Rsc");
7
8// Assign the resource desired task
9ResourceAssignment assignment = project.ResourceAssignments.Add(task, resource);

Creating Multiple Resource Assignments

The following code example demonstrates how to set multiple resource assignments for one task and set user-defined start and end date for the resources.

 1Project project = new Project("New Project.mpp")
 2{
 3    CalculationMode = CalculationMode.Automatic
 4};
 5
 6project.Set(Prj.DateFormat, DateFormat.DateDddMmDdYy);
 7project.Set(Prj.StartDate, new DateTime(2019, 9, 16, 9, 0, 0));
 8project.Set(Prj.NewTasksAreManual, false);
 9project.Set(Prj.ActualsInSync, false);
10
11Resource workResource = project.Resources.Add("Servente (Work)");
12workResource.Set(Rsc.Name, "Servente (Work)");
13workResource.Set(Rsc.Initials, "S");
14workResource.Set(Rsc.Type, ResourceType.Work);
15workResource.Set(Rsc.StandardRateFormat, RateFormatType.Hour);
16workResource.Set(Rsc.Code, "1503");
17
18Resource materialResource = project.Resources.Add("Tijolo (Material)");
19materialResource.Set(Rsc.Name, "Tijolo (Material)");
20materialResource.Set(Rsc.Initials, "T");
21materialResource.Set(Rsc.Type, ResourceType.Material);
22materialResource.Set(Rsc.StandardRateFormat, RateFormatType.MaterialResourceRate);
23materialResource.Set(Rsc.Code, "21341");
24
25Task task1 = project.RootTask.Children.Add("Task - 01");
26task1.Set(Tsk.IsRollup, new NullableBool(true));
27task1.Set(Tsk.IsPublished, new NullableBool(false));
28Task task2 = task1.Children.Add("Task - 01.01");
29task2.Set(Tsk.IsRollup, new NullableBool(true));
30task2.Set(Tsk.IsPublished, new NullableBool(false));
31Task task3 = task2.Children.Add("Task - 01.01.001");
32task3.Set(Tsk.IsEstimated, new NullableBool(false));
33task3.Set(Tsk.Start, new DateTime(2019, 9, 16, 9, 0, 0));
34task3.Set(Tsk.Duration, project.GetDuration(10, TimeUnitType.Day));
35task3.Set(Tsk.Work, project.GetDuration(10, TimeUnitType.Day));
36task3.Set(Tsk.IsRollup, new NullableBool(true));
37task3.Set(Tsk.IsPublished, new NullableBool(false));
38
39ResourceAssignment assignment1 = project.ResourceAssignments.Add(task3, materialResource);
40assignment1.Set(Asn.Delay, project.GetDuration(40, TimeUnitType.Hour));
41assignment1.Set(Asn.Start, new DateTime(2019, 9, 23, 9, 0, 0));
42assignment1.Set(Asn.Finish, new DateTime(2019, 9, 27, 18, 0, 0));
43ResourceAssignment assignment2 = project.ResourceAssignments.Add(task3, workResource);
44assignment2.Set(Asn.Work, project.GetDuration(56, TimeUnitType.Hour));
45assignment2.Set(Asn.Start, new DateTime(2019, 9, 16, 9, 0, 0));
46assignment2.Set(Asn.Finish, new DateTime(2019, 9, 24, 18, 0, 0));
47
48// to match expected MPP fully
49assignment2.Set(Asn.WorkContour, WorkContourType.Contoured);
50task3.Set(Tsk.IsManual, new NullableBool(true));
51task1.Set(Tsk.IsManual, new NullableBool(true));
52
53project.Save("Assignment_Dates.mpp", SaveFileFormat.MPP);
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.