Working with Tasks
Sometimes it is not possible to complete a task as scheduled, and it needs to be split into two or more parts. Microsoft Project supports task splitting, and Aspose.Tasks for .NET provides the same functionality programmatically.
Splitting Tasks
The SplitParts property exposed by the Task class is used to determine the split parts of a task whereas SplitTask method exposed by the ResourceAssignment class is used to split a single task into multiple parts. SplitParts returns a collection of split parts whereas SplitTask method accepts start date, finish date and calendar arguments to split the task.
Splitting and Viewing Tasks in Microsoft Project
In Microsoft Project, you can split a task as follows:
- Select a long task that you want to divide.
- On the Task tab, in the Schedule group, click Split Task.
- Click at the point on the task bar where you want the split to occur.
Split tasks are displayed in the Gantt Chart view and are linked by a dotted line.
Example: Creating and Splitting a Task
The following example demonstrates how to create a project, add a task, assign a resource, and split the task programmatically:
1// Create new project
2Project splitTaskProject = new Project();
3
4// Get a standard calendar
5Calendar calendar = splitTaskProject.Get(Prj.Calendar);
6
7// Set project's calendar settings
8splitTaskProject.Set(Prj.StartDate, new DateTime(2000, 3, 15, 8, 0, 0));
9splitTaskProject.Set(Prj.FinishDate, new DateTime(2000, 4, 21, 17, 0, 0));
10
11// Add a new task to root task
12Task rootTask = splitTaskProject.RootTask;
13rootTask.Set(Tsk.Name, "Root");
14Task taskToSplit = rootTask.Children.Add("Task1");
15taskToSplit.Set(Tsk.Duration, splitTaskProject.GetDuration(3));
16
17// Create a new resource assignment and generate timephased data
18ResourceAssignment splitResourceAssignment = splitTaskProject.ResourceAssignments.Add(taskToSplit, null);
19splitResourceAssignment.TimephasedDataFromTaskDuration(calendar);
20
21// Split the task into 3 parts.
22// Provide start date and finish date arguments to SplitTask method which will be used for split
23splitResourceAssignment.SplitTask(new DateTime(2000, 3, 16, 8, 0, 0), new DateTime(2000, 3, 16, 17, 0, 0), calendar);
24splitResourceAssignment.SplitTask(new DateTime(2000, 3, 18, 8, 0, 0), new DateTime(2000, 3, 18, 17, 0, 0), calendar);
25splitResourceAssignment.Set(Asn.WorkContour, WorkContourType.Contoured);
26
27splitTaskProject.Save("CreateSplitTasks_out.xml", SaveFileFormat.XML);
Example: Viewing Split Tasks
The next example shows how to identify split tasks in a project and print their details to the console:
1Project project = new Project("New Project.mpp");
2
3// Access task
4Task splitTask = project.RootTask.Children.GetById(4);
5
6// Display split parts of task
7SplitPartCollection collection = splitTask.SplitParts;
8foreach (SplitPart splitPart in collection)
9{
10 Console.WriteLine("Index: " + splitPart.Index + " Start: " + splitPart.Start + " Finish: " + splitPart.Finish);
11}
Key Notes
- Splitting tasks helps model real-world interruptions in project schedules.
- The
SplitTask
method does not delete work — it redistributes the existing work into separate intervals. - The
SplitParts
property allows developers to analyze and process each segment individually. - Works consistently with both MPP and XML project formats.
FAQ
Q: Can a task have more than two splits?
- Yes. A task can be divided into multiple segments.
Q: Does splitting affect task duration?
- The total duration changes depending on the placement of split intervals, but total work remains unchanged.
Q: Can split tasks be merged back?
- In Microsoft Project, you can manually adjust bars to remove splits. In Aspose.Tasks, you can manage this by resetting task assignments or recreating the task.
Q: Is this feature supported for recurring tasks?
- No. Splitting is intended for regular tasks, not recurring task definitions.