Hello,
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?
Thanks!!
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
task.Children.Add(subtask)
'Adding predecessors. Concatenate the subtasks, so that one subtask
'is perfomed after another in the time line.
If j = 1 Then
presub = subtask
Else
Dim tsklnk As TaskLink = New TaskLink(presub, subtask, TaskLinkType.FinishToStart)
project.AddTaskLink(tsklnk)
presub = subtask
End If
Next j
rootTask.Children.Add(task)
Next i
'Set rootTask as root task of the project
project.RootTask = rootTask
project.CalcTaskIds()
project.CalcTaskUids()
project.UpdateReferences()
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
Else
proj.AddTaskLink(New TaskLink(Pred, Subtask, TaskLinkType.FinishToStart))
Pred = Subtask
End If
Next
Next
proj.Save(“D:\Aspose\output.xml”, SaveFileFormat.XML)
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
Else
proj.AddTaskLink(New TaskLink(Pred, Subtask, TaskLinkType.FinishToStart))
Pred = Subtask
End If
Next
Next
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)
ParentTask.OutlineOutdent()
Dim Subtask As Aspose.Tasks.Task = proj.AddTask("Subtask " & 1)
Subtask.Duration = New TimeSpan(24, 0, 0)
Subtask.OutlineIndent()
For j As Integer = 2 To 10
Subtask = proj.AddTask("Subtask " & j)
Subtask.Duration = New TimeSpan(24, 0, 0)
Next
Next
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.
Thanks
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.