Creating Resource Assignments

Resource assignments represent the connection between tasks and resources. They define how much of a resource’s time or effort will be spent on a particular task. Aspose.Tasks for .NET provides several ways to create resource assignments, giving developers the flexibility to manage project data programmatically without Microsoft Project.

Creating Resource Assignments

The Resource class allows creating assignments in two ways:

  1. By using the default constructor.
  2. By passing a task and resource when instantiating a ResourceAssignment.

Programming Sample: Default Constructor

In this example, we create a ResourceAssignment instance without passing any parameters. This approach is useful when you want to initialize an assignment first and set its properties later.

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

You can also assign multiple resources to a single task. In the following example, we demonstrate how to create several resource assignments for one task and define custom start and finish dates for each resource.

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

FAQ

Q: Do I need Microsoft Project installed to create resource assignments?

Q: Can I assign multiple resources to the same task?

Q: Will resource assignments created with Aspose.Tasks be visible in Microsoft Project?

Conclusion

Creating resource assignments with Aspose.Tasks for .NET allows developers to programmatically link tasks and resources, manage workloads, and define flexible assignment parameters. Whether you are initializing a simple assignment or configuring multiple resources for a single task, Aspose.Tasks provides the tools to handle these operations seamlessly in C# without relying on Microsoft Project.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.