We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Problems with tasklinks in a loop

I’m having problems assigning tasklinks between subtasks in a loop. I’m trying to write a xml project and I would like to concatenate all the subtasks, I mean to add as predecessor the subtask before.
When I open the *.xml in MS project, only the subtask 2 has a predecessor (subtask 1).
Any idea about the problem?


Here is the code:

'Creates a project instance
Dim project As New Project()
Dim dataDir As String = Path.GetFullPath(“c:”)
Dim rootTask As New Task
Dim pre As Task = Nothing

'Dim task As New Task

Dim i As Integer
Dim j As Integer

For i = 1 To 10

Dim task As New Task("Example Task " & i)
Dim presub As Task = Nothing

For j = 1 To 10

Dim subtask As New Task("Subtask " & j)
'Set the subtasks duration = ’ hours for every sub task
subtask.Duration = New TimeSpan(24, 0, 0)
subtask.DurationFormat = TimeUnitType.Day

'Set task as parent Task

'Adding predecessors. Concatenate the subtasks, so that one subtask
'is perfomed after another in the time line.

If j = 1 Then
presub = subtask
Dim tsklnk As TaskLink = New TaskLink(presub, subtask, TaskLinkType.FinishToStart)
presub = subtask
End If

Next j


Next i

'Set rootTask as root task of the project
project.RootTask = rootTask



project.Save(dataDir & “Project1.xml”, Aspose.Tasks.Saving.SaveFileFormat.XML)

Hi Katherine,

Thank you for contacting Aspose support.

It is recommended that you may please use Project.AddTask and Project.AddTaskLink for adding respective item. Following is the sample code which generates similar output as required by you:

Dim proj As New Project()
For i As Integer = 1 To 10
Dim ParentTask As Aspose.Tasks.Task = proj.AddTask("Example Task " & i)
ParentTask.OutlineLevel = 1
Dim Pred As New Aspose.Tasks.Task()
Pred.Duration = New TimeSpan(24, 0, 0)
For j As Integer = 1 To 10
Dim Subtask As Aspose.Tasks.Task = proj.AddTask("Subtask " & j)
Subtask.Duration = New TimeSpan(24, 0, 0)
Subtask.OutlineLevel = 2
If j = 1 Then
Pred = Subtask
proj.AddTaskLink(New TaskLink(Pred, Subtask, TaskLinkType.FinishToStart))
Pred = Subtask
End If
proj.Save(“D:\Aspose\output.xml”, SaveFileFormat.XML)

Thank you for this recommendation kashif , it does what I’m trying to do. However I noticed that this solution is slower than mine, am I correct?
This is just an example to learn about how to use the library, but I’ll probably use it for a bigger application with a lot of information to read (from a database). Do you have any recommandations then?

Katherine GM

Hi Katherine,

There are multiple ways of achieving the required output. We can indent tasks using OutlineLevel or OutlineIndent/OutlineOutdent funstions as demonstrated in the following sample code. For making a task parent, you may use OutlineOutdent() function and for setting a task to child, call the function OutlineIndent(). Once we call any of these functions, rest of the tasks are created on the same indentation level. It is your choice to use either OutlineLevel or Indentation functions as per your ease.

Dim proj As New Project()

For i As Integer = 1 To 10

Dim ParentTask As Aspose.Tasks.Task = proj.AddTask("Example Task " & i)


Dim Subtask As Aspose.Tasks.Task = proj.AddTask("Subtask " & 1)

Subtask.Duration = New TimeSpan(24, 0, 0)


For j As Integer = 2 To 10

Subtask = proj.AddTask("Subtask " & j)

Subtask.Duration = New TimeSpan(24, 0, 0)



proj.Save(“D:\Aspose\output2.xml”, SaveFileFormat.XML)

Yes Kashif, but it didn’t answer my question:

  • What is the difference between : using OutlineIndent/OutlineOutdent functions , AND using Children/RootTask relations?
  • Why do you recommend to use Indents?
  • Is there any difference related to the execution speed?

I couldn’t find the answer in the API documentation.


Hi Katherine,

  • OultineIndent/OutlineOutdent methods usually do the following jobs
    • Set parent/child relation
    • Recalculate Ids
    • Recalculate dates if necessary
  • While the Children/RootTask is totally manual job without any recalculations. Client has to track all values, references and fields
  • We recommend the indentation method because the output of these methods is more predictable
  • Yes, the indentation methods are slower because they involve a lot of calculations
Please let us know if we can be of any additional help to you in this regard.