How to complete a created task

Hi there,

I've created a plan containing tasks with a variety of durations, all allocated to a variety of resources.

How do I set a task I've created to be completed? I've tried setting the task.percentcomplete to 100, but this doesn't seem to make any difference to the task on the generated plan.

Could you please let me know what I should be doing?

Many thanks,

Steve

Hi there (again)

Another (related) question please...

The tasks that I flag as completed also need to have an 'actual' start and end date set. I've tried using 'actual' or 'manual' start and finish, but it just leaves a task of duration 0, finishing on 1 Jan 2011.

Again, any help would be much appreciated :)

Cheers,

Steve

Hi Steve,

In my case the code below works:

using System;
using Aspose.Tasks;
namespace TestCompleteTask
{
    class Program
    {
        static void Main(string[] args)
        {
            License license = new License();
            license.SetLicense("Aspose.Tasks.lic");
            ProjectReader reader = new ProjectReader();
            Project project = reader.Read("Project1.xml");
            Task task = project.GetTaskByUid(1);
            task.PercentComplete = 100;
            task.ActualStart = task.Start;
            task.ActualFinish = task.Finish;
            task.ActualDuration = task.Duration;
            task.ActualWork = task.Work;
            task.ActualCost = task.Cost;
            ProjectWriter writer = new ProjectWriter();
            writer.Write(project, "Project2.xml", TasksDataFormat.XML);
        }
    }
}

In case your task has children they have to be completed too.
See test data and screen shot attached.

Hi,

Thanks for the response. Unfortunately, I still can't add a completed task.

The code I use:

Dim oTask As New Task

oTask.Type = TaskType.FixedUnits

If (oRow.Item("TaskIsCompleted") & "") = "True" Then

oTask.PercentComplete = 100

oTask.ActualStart =dtStartTime

oTask.ActualFinish = dtEndTime

oTask.ActualDuration = New TimeSpan(lEffortInHours, 0, 0)

Else

oTask.Duration = New TimeSpan(lEffortInHours, 0, 0)

oTask.DurationFormat = TimeUnitType.Day

oTask.RemainingDuration = New TimeSpan(lEffortInHours, 0, 0)

oTask.Deadline = dtCompletionDate

oTask.ConstraintDate = dtCompletionDate

oTask.ConstraintType = ConstraintType.MustFinishOn

End If

This works fine for non-completed jobs, but creates 0 duration tasks at 31st Dec 2010 for completed jobs.

Any advice would be much appreciated,

Cheers,

Steve

Hi Steve,

I have used the code below to create a completed new task to the project from my previous post:

using System;
using Aspose.Tasks;
namespace TestCompleteTask
{
    class Program
    {
        static void Main(string[] args)
        {
            License license = new License();
            license.SetLicense("Aspose.Tasks.lic");
            ProjectReader reader = new ProjectReader();
            Project project = reader.Read("Project1.xml");
            Calendar cal = project.Calendar;
            //Create a new completed task.
            Task task = new Task("New completed task.");
            task.Uid = project.NextTaskUid;
            task.Type = TaskType.FixedUnits;
            task.Start = new DateTime(2010, 12, 15, 8, 0, 0);
            task.Duration = new TimeSpan(80, 0, 0);
            task.Finish = project.Calendar.GetFinishDateByStartAndWork(task.Start, task.Duration);
            task.DurationFormat = TimeUnitType.Day;
            task.ActualStart = task.Start;
            task.ActualFinish = task.Finish;
            task.ActualDuration = task.Duration;
            task.PercentComplete = 100;
            //Have to add an empty resource assignments to the task to make it completed.
            ResourceAssignment ra = new ResourceAssignment();
            ra.Uid = project.NextResourceAssignmentUid;
            ra.Task = task;
            ra.Start = task.Start;
            ra.Finish = task.Finish;
            ra.TimephasedDataFromTaskDuration(cal);
            project.ResourceAssignments.Add(ra);
            //Complete old task.
            Task old_task = project.GetTaskByUid(1);
            old_task.PercentComplete = 100;
            old_task.ActualStart = task.Start;
            old_task.ActualFinish = task.Finish;
            old_task.ActualDuration = task.Duration;
            project.RootTask.Children.Add(task);
            project.CalcTaskIds();
            project.CalcResourceAssignmentIds();
            ProjectWriter writer = new ProjectWriter();
            writer.Write(project, "Project2.xml", TasksDataFormat.XML);
        }
    }
}

Now both tasks are shown as completed in MS Project 2003/2007/2010.

I have created a new issue “Add a method to change task progress.” with ID = 22450 and linked it to this forum thread. We are going to add a new method to change task’s progress automatically.

Sorry for the inconvenience.

Sergey,

Hi, thanks for that.

I can now get new tasks completed with empty resource allocations. My remaining problem is that we still need to allocate existing resources to these tasks- but as soon as I try allocating them, the tasks become incomplete.

Could you amend the code you wrote above for me so that it had an actual resource allocated to the task?

Thanks,

Steve

Hi Steve,

Try to use the code below:

using System;
using Aspose.Tasks;

namespace TestCompleteTask
{
    class Program
    {
        static void Main(string[] args)
        {
            License license = new License();
            license.SetLicense("Aspose.Tasks.lic");
            ProjectReader reader = new ProjectReader();
            Project project = reader.Read("Project1.xml");
            Calendar cal = project.Calendar;
            //Create a new completed task.
            Task task = new Task("New completed task.");
            task.Uid = project.NextTaskUid;
            task.Type = TaskType.FixedUnits;
            task.Start = new DateTime(2010, 12, 15, 8, 0, 0);
            task.Duration = new TimeSpan(80, 0, 0);
            task.Finish = cal.GetFinishDateByStartAndWork(task.Start, task.Duration);
            task.DurationFormat = TimeUnitType.Day;
            task.ActualStart = task.Start;
            task.ActualFinish = task.Finish;
            task.ActualDuration = task.Duration;
            task.PercentComplete = 100;
            //Create a new resource.
            Resource resource = new Resource();
            resource.Name = "Employee";
            resource.Type = ResourceType.Work;
            resource.Work = task.Duration;
            resource.ActualWork = resource.Work;
            resource.Uid = project.NextResourceUid;
            project.Resources.Add(resource);
            //Create a resource assignment
            ResourceAssignment ra = new ResourceAssignment();
            ra.Uid = project.NextResourceAssignmentUid;
            ra.Task = task;
            ra.Resource = resource;
            ra.ActualWork = ra.Work = resource.Work;
            ra.Start = task.Start;
            ra.Finish = task.Finish;
            ra.Units = 1;
            ra.TimephasedDataFromTaskDuration(cal);
            project.ResourceAssignments.Add(ra);
            project.RootTask.Children.Add(task);
            project.CalcTaskIds();
            project.CalcResourceIds();
            project.CalcResourceAssignmentIds();
            ProjectWriter writer = new ProjectWriter();
            writer.Write(project, "Project2.xml", TasksDataFormat.XML);
        }
    }
}

It works OK with me.

Hi Sergey,

I tried using your code, and I can now get it almost working!

The remaining problem is that all tasks that I need to set as complete are only set to 99% complete. If I set it to 100% by hand in MS Project, it then completes OK.

Is this an issue that you have seen before?

Cheers,

Steve

I tried to go back to the bare basics, and get your code to work.

I tried to amend it so that it would create a project from scratch, but couldn't get the following code to work. Any help would be appreciated (again!). Cheers, Steve

Dim license3 As Aspose.Tasks.License = New Aspose.Tasks.License
license3.SetLicense(HttpContext.Current.Server.MapPath("Aspose.Tasks.lic"))

Dim oProject As New Project
oProject.WorkFormat = TimeUnitType.Hour
oProject.MinutesPerDay = 60 * 8

Dim cal As Aspose.Tasks.Calendar = New Aspose.Tasks.Calendar()
cal.Name = "Calendar24x7"
Aspose.Tasks.Calendar.MakeStandardCalendar(cal)

'add working days monday through thursday with default timings
cal.Days.Add(WeekDay.CreateDefaultWorkingDay(DayType.Monday))
cal.Days.Add(WeekDay.CreateDefaultWorkingDay(DayType.Tuesday))
cal.Days.Add(WeekDay.CreateDefaultWorkingDay(DayType.Wednesday))
cal.Days.Add(WeekDay.CreateDefaultWorkingDay(DayType.Thursday))
cal.Days.Add(WeekDay.CreateDefaultWorkingDay(DayType.Friday))
cal.Days.Add(WeekDay.CreateDefaultWorkingDay(DayType.Saturday))
cal.Days.Add(WeekDay.CreateDefaultWorkingDay(DayType.Sunday))

oProject.Calendars.Add(cal)
oProject.CalcCalendarUids()

Dim Task As New Task("New completed task.")
Task.Calendar = cal
Task.Uid = oProject.NextTaskUid
Task.Type = TaskType.FixedUnits
Task.Start = New DateTime(2010, 12, 15, 8, 0, 0)
Task.Duration = New TimeSpan(80, 0, 0)
Task.Finish = cal.GetFinishDateByStartAndWork(Task.Start, Task.Duration)
Task.DurationFormat = TimeUnitType.Day

Task.ActualStart = Task.Start
Task.ActualFinish = Task.Finish
Task.ActualDuration = Task.Duration
Task.PercentComplete = 100

Dim Resource As New Resource()
Resource.Name = "Employee"
Resource.Type = ResourceType.Work
Resource.Work = Task.Duration
Resource.ActualWork = Resource.Work
Resource.Uid = oProject.NextResourceUid
oProject.Resources.Add(Resource)

Dim ra As New ResourceAssignment()
ra.Uid = oProject.NextResourceAssignmentUid
ra.Task = Task
ra.Resource = Resource
ra.ActualWork = Resource.Work
ra.Work = Resource.Work
ra.Start = Task.Start
ra.Finish = Task.Finish
ra.Units = 1
ra.TimephasedDataFromTaskDuration(cal)
oProject.ResourceAssignments.Add(ra)

oProject.RootTask.Children.Add(Task)

oProject.CalcTaskIds()
oProject.CalcResourceIds()
oProject.CalcResourceAssignmentIds()

Dim prjWriter As New ProjectWriter()

Me.Response.ContentType = "application/vnd.ms-project"
Me.Response.AppendHeader("Content-Disposition", "attachment; filename=project.xml")
Me.Response.Flush()
Dim st As System.IO.Stream = Me.Response.OutputStream
prjWriter.Write(oProject, st, TasksDataFormat.XML)
Me.Response.End()

Hi,

I have seen the 99% problem before and have not had time to track down the problem. I appreciate your taking the time to do this!!!!!

I also did not realise that an assignment was necessary to mark a task as complete.

MS Project VBA is very helpful when setting completion dates or % completes... I believe there is a lot of data integrity checks that are made when certain actions are taken to keep everything consistent.

Many thanks for digging into this area!!!

Regards, Bruce

Hi All,


I have tested the code above with MS Project 2003/2007/2010 and the tasks are shown as 100% completed. Could you share your code and data samples to check the issue on our side?

Any information is highly appreciated! Thank you for your cooperation!

The empty assignment is not always necessary, but MS Project creates it for each new task (to track the task duration, work and splits) so I just followed it. In case if the task was created in MS Project the file contains the assignment.

I am going to create a sample for Steve’s case, but I would recommend to create a template and read it in XML format instead of creation in run time.

Sergey, Hi,

So the code I included above creates a project plan OK?

Meanwhile, I'll try reading in an empty mpp file first...

Cheers,

Steve

Hi Steve,


I have not tested your code yet, I just wrote about the 99% problem which I have not seen before…
Sorry for the misleading.

Hi,

I can now get a task generated from the following code, but the task is not completed. It may be due to me translating the c# line containing two ='s incorrectly...

Private Sub Test()

' Try
Dim license3 As Aspose.Tasks.License = New Aspose.Tasks.License
license3.SetLicense(HttpContext.Current.Server.MapPath("Aspose.Tasks.lic"))

Dim oRdr As New ProjectReader()
Dim oProject As Project = oRdr.Read(HttpContext.Current.Server.MapPath("~/ReportTemplates/Empty Template.mpp"))


' Dim oProject As New Project
oProject.WorkFormat = TimeUnitType.Hour
oProject.MinutesPerDay = 60 * 8

Dim cal As Aspose.Tasks.Calendar = oProject.Calendar ' New Aspose.Tasks.Calendar()

Dim Task As New Task("New completed task.")
Task.Calendar = cal
Task.Uid = oProject.NextTaskUid
Task.Type = TaskType.FixedUnits
Task.Start = New DateTime(2010, 12, 15, 8, 0, 0)
Task.Duration = New TimeSpan(80, 0, 0)
Task.Finish = cal.GetFinishDateByStartAndWork(Task.Start, Task.Duration)
Task.DurationFormat = TimeUnitType.Day

Task.ActualStart = Task.Start
Task.ActualFinish = Task.Finish
Task.ActualDuration = Task.Duration
Task.PercentComplete = 100

Dim Resource As New Resource()
Resource.Name = "Employee"
Resource.Type = ResourceType.Work
Resource.Work = Task.Duration
Resource.ActualWork = Resource.Work
Resource.Uid = oProject.NextResourceUid
oProject.Resources.Add(Resource)

Dim ra As New ResourceAssignment()
ra.Uid = oProject.NextResourceAssignmentUid
ra.Task = Task
ra.Resource = Resource
ra.ActualWork = Resource.Work
ra.Work = Resource.Work
ra.Start = Task.Start
ra.Finish = Task.Finish
ra.Units = 1
ra.TimephasedDataFromTaskDuration(cal)
oProject.ResourceAssignments.Add(ra)

oProject.RootTask.Children.Add(Task)

oProject.CalcTaskIds()
oProject.CalcResourceIds()
oProject.CalcResourceAssignmentIds()

Dim prjWriter As New ProjectWriter()

Me.Response.ContentType = "application/vnd.ms-project"
Me.Response.AppendHeader("Content-Disposition", "attachment; filename=project.xml")
Me.Response.Flush()
Dim st As System.IO.Stream = Me.Response.OutputStream
prjWriter.Write(oProject, st, TasksDataFormat.XML)
Me.Response.End()

End Sub

The issues you have found earlier (filed as TASKS-22450) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(1)

The issues you have found earlier (filed as ) have been fixed in this Aspose.Words for JasperReports 18.3 update.