Cannot set PercentComplete of a task

This is my code to add 1 task with a 50% percent complete - it doesnt matter what I do, the % complete appears as 0% when I open the XML file in MS Project. What am I doing wrong?

Aspose.Tasks.License asp = new Aspose.Tasks.License();
asp.SetLicense("Aspose.Tasks.lic");
Project prj = new Project();
prj.MinutesPerDay = 480;

Task rootTsk = new Task();
Task tsk1 = new Task("Task1");
tsk1.Start = new DateTime(2013,10,1);
tsk1.ActualStart = new DateTime(2013, 10, 1);

tsk1.Duration = new TimeSpan(10 * 8, 0, 0);
tsk1.DurationFormat = TimeUnitType.Day;

tsk1.PercentComplete = 50;
tsk1.PercentWorkComplete = 50;
prj.RootTask = rootTsk;
rootTsk.Children.Add(tsk1);
prj.CalcTaskIds();
prj.CalcTaskUids();
ProjectWriter prjWriter = new ProjectWriter();

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

Hi Steve,

We are sorry for a delayed response.

In order to set the percent complete value for a task, please use the SetPercentComplete method after the recalculations as shown in the example given below.

Sample Code:

Project prj = new Project();
prj.StartDate = new DateTime(2012, 8, 27);
Task Approval = new Task("Approval");
Task ScopeDocument = new Task("Scope document");
ScopeDocument.Start = new DateTime(2012, 8, 27);
ScopeDocument.Duration = new TimeSpan(0, 24, 0, 0);
ScopeDocument.ConstraintType = ConstraintType.StartNoEarlierThan;
ScopeDocument.ConstraintDate = new DateTime(2012, 8, 27);
Task ApprovalToProceed = new Task("Approval to proceed");
ApprovalToProceed.Start = new DateTime(2012, 8, 30);
ApprovalToProceed.Duration = new TimeSpan(0, 8, 0, 0);
ApprovalToProceed.ConstraintType = ConstraintType.StartNoEarlierThan;
ApprovalToProceed.ConstraintDate = new DateTime(2012, 8, 30);
Approval.Children.Add(ScopeDocument);
Approval.Children.Add(ApprovalToProceed);
prj.RootTask.Children.Add(Approval);
Recalculate(prj);
Task.Recalculate(prj.RootTask);
ScopeDocument.SetPercentComplete(60);
ApprovalToProceed.SetPercentComplete(90);
prj.Save("CreateNewTasks2.xml", Aspose.Tasks.Saving.SaveFileFormat.XML);

This wont compile :


You need to provide me the code for Recalculate(prj);

5 lines from the bottom

Hi Steve,


We are sorry for the inconvenience caused to you. Following is the sample code for Recalculate function:

private static void Recalculate(Project project)
{
project.CalcTaskIds();
project.CalcTaskUids();
project.CalcResourceIds();
project.CalcResourceUids();
project.CalcResourceAssignmentIds();
project.CalcResourceAssignmentUids();
project.CalcCalendarUids();
project.UpdateReferences();
}



Thanks for the quick reply, however the routine now crashes on line

Line 1376:		Task.Recalculate(prj.RootTask);
Source File: d:\Development\p2\Horizon\Trunk\content\tools.aspx.cs Line: 1376

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]


However, if I step over this line, it appears to work and I get correct percent complete values when i open the xml file in MS Project
Hi Steve,

I have tested the code and found that this exception is there while using Aspose.Tasks for .NET 5.9.0. However, I would like to share that this exception doesn't occur with the upcoming version of Aspose.Tasks for .NET 6.0.0, whihc we are in process of release. I would request you to please spare us a litttle time for product release completion that will solve this issue for you.

Hi I’ve now downloaded version 6.0 and whatever I do I cant get the code to create an export to XML with percent completes and resources. If I have created a simple 1 task plan and used SetPercentComplete(60) (say) then the xml file created correctly opens in MS project with the right data and my task is 60% complete


As soon as I add an assignment to that task (in my code), when I open in MS Project the task reduces to a 0 duration task and no percent complete. could you give me some sample code that

1. creates a project using the standard calendar
2. adds 1 task called ‘Task 1’ which is 2 days long
3. sets percent complete of that task to 50%
4. adds a resource to it.
5. Exports to XML.

So that when i open it in MS project all that data is correctly displayed, Ive tried everything now and am on the verge of throwing my PC out of the window.

Hi Steve,


We are sorry for the inconvenience caused to you.

Aspose.Tasks has simplified the process of creating task, resource and assignment. Please have a look at the following sample code which creates task, sets its duration, percent complete and adds resource to it. The output file opens successfully in MSP and displays %complete as required. Most of the default settings are done automatically when Project instance is initiated including standard calendar.

Project project = new Project();
Task task1 = project.AddTask(“Task1”); // adds after the last project’s task
task1.Start = new DateTime(2013, 11, 25);
task1.Duration = new TimeSpan(16, 0, 0);
task1.SetPercentComplete(50);
Resource rsc = project.AddResource(“Resource 1”);
ResourceAssignment assgn = new ResourceAssignment(task1, rsc);
project.AddResourceAssignment(task1, rsc);
project.Save(“TestXml20.xml”, Aspose.Tasks.Saving.SaveFileFormat.XML);

Thanks for the quick reply, it was all going so well until I tried adding a second task as a ‘child’ of task 1 so…


Task task2 = new Task(“Task 2”);
task2.Start = new DateTime(2013, 11, 25);
task2.Duration = new TimeSpan(32, 0, 0);
task2.SetPercentComplete(70);
task1.Children.Add(task2);

where task1 is the task created using your code above. Unfortunately this exported to MS project with just Task1 (no sign of task2) in fact Task1 was now rendered on the second row of the project so there was a blank in row 1.

Can you help by showing if theres a new way of adding ‘child tasks’

Hi Steve,


Thank you for feedback.

The following code sample can be used for including both the tasks in the required order. However, this sets percent complete to 0. I have logged this issue as TASKS-33497 for further investigation by development team and will update you once the fix version is available in this regard.

Sample Code:


Project project = new Project();

Task task1 = project.AddTask(“Task1”); // adds after the last project’s task

task1.Start = new DateTime(2013, 11, 25);

task1.Duration = new TimeSpan(16, 0, 0);


//Define task 2

Task task2 = project.AddTask(“Task2”);

Resource rsc = project.AddResource(“Resource 1”);

project.AddResourceAssignment(task2,rsc);

task2.SetPercentComplete(70);

task2.OutlineIndent();

project.Save(“TestXml20.xml”, Aspose.Tasks.Saving.SaveFileFormat.XML);



Thanks for the reply. I’ll wait until the fix is available before carrying on. I need to be sure it will work before I commit too much more time. Hopefully the fix will be available very soon

Hi Steve,


As you may see from the status of this issue we have already fixed it and plan to release the fix version at the end of this month. As soon as the fix version is available, you’ll be automatically notified here via this thread.

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


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

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