- Start Visual Studio and create a new project. For this example I will create a Web Application.
- Once you have created your project, add a reference to the Aspose.Tasks. If you do not know how to add a reference then pleas visit our How to add a component reference tutorial.
- Then double click on WebForm1.aspx to view the WebForm1.aspx.cs code behind file.
- Under the last using (C#), or Imports (Visual Basic) statement at the top of the file add using Aspose.Tasks;(C#), or Imports Aspose.Tasks (Visual Basic) like the following example.
[C#]
using Aspose.Tasks;
[VB]
Imports Aspose.Tasks
- After that return to WebForm1.aspx and add a button. Change it's label to "Get Project". This button will be used to execute the code that will modify our presentation and allow the changes to be saved
- Next double click the newly created button within the WebForm1.aspx window to take you to Button1's click event procedure.
[C#]
private void Button1_Click(object sender, System.EventArgs e)
{
}
[VB]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
- In this example we will create a project in MPX format. To do this add the following line of code to the Button1's click event procedure.
[C#]
MPXFile file = new MPXFile ();
[VB]
Dim file As MPXFile = New MPXFile ();
- After that change project's default options to automatic generation of Calendar's, Task's and Resource's id numbers, outline levels and WBS labels:
[C#]
file.AutoCalendarUniqueID = true;
file.AutoTaskID = true;
file.AutoTaskUniqueID = true;
file.AutoResourceID = true;
file.AutoResourceUniqueID = true;
file.AutoOutlineLevel = true;
file.AutoOutlineNumber = true;
file.AutoWBS = true;
[VB]
file.AutoCalendarUniqueID = True
file.AutoTaskID = True
file.AutoTaskUniqueID = True
file.AutoResourceID = True
file.AutoResourceUniqueID = True
file.AutoOutlineLevel = True
file.AutoOutlineNumber = True
file.AutoWBS = True
- Next create a default calendar. In such calendar default workdays are Monday - Friday and worktime is 8:00 – 12:00, 13:00 – 17:00
[C#]
file.AddDefaultBaseCalendar();
[VB]
file.AddDefaultBaseCalendar()
- After that set project title and Start date
[C#]
MPXHeader prj = file.Project;
prj.StartDate = new System.DateTime(2000, 1, 1);
prj.Title = "Project Title";
[VB]
Dim prj As MPXHeader = file.Project
prj.StartDate = New System.DateTime(2000, 1, 1)
prj.Title = "Project Title"
- Now add 2 resources to project:
[C#]
Resource resource1 = file.AddResource();
resource1.Name = "Resource1";
resource1.AvailableFrom = new System.DateTime(2000, 1, 13);
resource1.AvailableTo = new System.DateTime(2000, 1, 16);
Resource resource2 = file.AddResource();
resource2.Name = "Resource2";
resource2.AvailableFrom = new System.DateTime(2000, 1, 13);
resource2.AvailableTo = new System.DateTime(2000, 1, 30);
[VB]
Dim resource1 As Resource = file.AddResource()
resource1.Name = "Resource1"
resource1.AvailableFrom = New System.DateTime(2000, 1, 13);
resource1.AvailableTo = New System.DateTime(2000, 1, 16);
Dim resource2 As Resource = file.AddResource()
resource2.Name = "Resource2"
resource2.AvailableFrom = New System.DateTime(2000, 1, 13);
resource2.AvailableTo = New System.DateTime(2000, 1, 30);
- Next step is adding a Summary Task. Every project has such task from which others are inherited. It is hidden by default.
[C#]
Task task1 = file.AddTask();
task1.Name = "Summary Task";
[VB]
Dim task1 As Task = file.AddTask()
task1.Name = "Summary Task"
- Next we will create a first task:
[C#]
Task task2 = task1.AddTask();
task2.Name = "First Sub Task";
task2.Duration = new Duration(10, TimeUnit.Days);
task2.Start = new System.DateTime(2000, 1, 1);
[VB]
Dim task2 As Task = task1.AddTask()
task2.Name = "First Sub Task"
task2.Duration = New Duration(10, TimeUnit.Days)
task2.Start = New System.DateTime(2000, 1, 1)
- We'll set this task up as being 50% complete. If we have no resource assignments for this task, this is enough information for MS Project. If we do have resource assignments, the assignment record needs to contain the corresponding work and actual work fields set to the correct value1s in order for MS project to mark the task as complete or partially complete.
[C#]
task2.PercentComplete = 50.0;
task2.ActualStart = new System.DateTime(2000, 1, 1);
[VB]
task2.PercentComplete = 50.0
task2.ActualStart = New System.DateTime(2000, 1, 1)
[C#]
Task task3 = task1.AddTask();
task3.Name = "Second Sub Task";
task3.Start = new System.DateTime(2000, 1, 11);
task3.Duration = new Duration(10, TimeUnit.Days);
[VB]
Dim task3 As Task = task1.AddTask()
task3.Name = "Second Sub Task"
task3.Start = New System.DateTime(2000, 1, 11)
task3.Duration = New Duration(10, TimeUnit.Days)
- We'll link these tasks so one is started after another:
[C#]
Relation rel1 = task3.AddPredecessor(task2);
rel1.Type = TaskLinkType.FinishToStart;
[VB]
Dim rel1 As Relation = task3.AddPredecessor(task2)
rel1.Type = TaskLinkType.FinishToStart
- After that add a milestone and link it with the second task:
[C#]
Task milestone1 = task1.AddTask();
milestone1.Name = "Milestone";
milestone1.Start = new System.DateTime(2000, 1, 21);
milestone1.Duration = new Duration(0, TimeUnit.Days);
Relation rel2 = milestone1.AddPredecessor(task3);
rel2.Type = TaskLinkType.FinishToStart;
[VB]
Dim milestone1 As Task = task1.AddTask()
milestone1.Name = "Milestone"
milestone1.Start = New System.DateTime(2000, 1, 21)
milestone1.Duration = New Duration(0, TimeUnit.Days)
Dim rel2 As Relation = milestone1.AddPredecessor(task3)
rel2.Type = TaskLinkType.FinishToStart
- Now assign resources to tasks:
[C#]
ResourceAssignment assignment1 = task2.AddResourceAssignment(resource1);
ResourceAssignment assignment2 = task3.AddResourceAssignment(resource2);
[VB]
Dim assignment1 As ResourceAssignment = task2.AddResourceAssignment(resource1)
Dim assignment2 As ResourceAssignment = task3.AddResourceAssignment(resource2)
- As the first task is partially complete, and we are adding a resource assignment, we must set the work and actual work fields in the assignment to appropriate value1s, or MS Project won't recognize the task as being complete or partially complete:
[C#]
assignment1.Work = new Duration(80, TimeUnit.Hours);
assignment1.ActualWork = new Duration(40, TimeUnit.Hours);
[VB]
assignment1.Work = New Duration(80, TimeUnit.Hours)
assignment1.ActualWork = New Duration(40, TimeUnit.Hours)
- Creation of project is completed and now we can send it to our browser:
[C#]
this.Response.ContentType = "application/vnd.ms-project";
this.Response.AppendHeader("Content-Disposition", "attachment; filename=project.mpx");
this.Response.Flush();
System.IO.Stream st = this.Response.OutputStream;
file.Write(st);
this.Response.End();
[VB]
Me.Response.ContentType = "application/vnd.ms-project"
Me.Response.AppendHeader("Content-Disposition", "attachment; filename=project.mpx")
Me.Response.Flush()
Dim st As System.IO.Stream = Me.Response.OutputStream
file.Write(st)
Me.Response.End()
- After your modifications are complete run you Web Application and click the "Get Project” button. A dialog box will pop up asking if you want to open or save the file. You can do either. If you saved the file you can now view it with Microsoft Project. Your end result should look like mine. If it does, congratulation you have created your first Project using Aspose.Tasks! Please continue on to our Programmer’s Guide section for more information and code examples.