Hi,
I’m trying to create a project file from DB data, but when I try open a file it throws an error.
I went to http://www.aspose.com/demos/.net-components/aspose.tasks/csharp/create-project.aspx
for demo, but even that code doesn’t work. Also when I run your demo it fails.
Is your demo is up to date? I’m using the latest version and just want a minimal project plan with tasks. If that demo isn’t latest can you please post something up to date that works and adds few tasks to project plan.
Regards,
Peter
Hi Peter,
//Create new project and set its properties
Project project = new Project();
project.StartDate = new DateTime(2012, 1, 1, 8, 0, 0);
project.FinishDate = new DateTime(2013, 12, 31, 17, 0, 0);
project.MinutesPerDay = 8 * 60;
project.MinutesPerWeek = 5 * 8 * 60;
project.DaysPerMonth = 20;
project.DurationFormat = TimeUnitType.Hour;
project.WorkFormat = TimeUnitType.Hour;
//Create a calendar for the project
Aspose.Tasks.Calendar standard = new Aspose.Tasks.Calendar(“Standard”);
standard = Aspose.Tasks.Calendar.MakeStandardCalendar(standard);
project.Calendars.Add(standard);
project.Calendar = standard;
//Create root task.
Task root = new Task();
root.Uid = project.NextTaskUid;
project.RootTask = root;
//Add child task
Task task1 = new Task(“Task1”);
task1.ActualStart = DateTime.Parse(“15-Aug-2012 8:00 AM”);
//set duration in hours
task1.Duration = new TimeSpan(8, 0, 0);
task1.DurationFormat = TimeUnitType.Day;
//Add task to the root task
root.Children.Add(task1);
//Add child task
Task task2 = new Task(“Task2”);
task2.ActualStart = standard.GetFinishDateByStartAndWork(task1.ActualStart, task1.Duration).AddDays(1).Date;
//set duration in hours
task2.Duration = new TimeSpan(24, 0, 0);
task2.DurationFormat = TimeUnitType.Day;
root.Children.Add(task2);
//Add tasks link
TaskLink tsklnk = new TaskLink(task1, task2, TaskLinkType.FinishToStart);
project.AddTaskLink(tsklnk);
//Perform calucaltions
project.CalcTaskIds();
project.CalcTaskUids();
project.CalcCalendarUids();
//Save the project
ProjectWriter writer = new ProjectWriter();
writer.Write(project, “Project1.xml”, TasksDataFormat.XML);
Hi Kashif,
thanks for respose.
I’ve tried your provided code, but it stilll throws the same error.
However I noticed you are saving file as .xml extension. Why is that? Ms Project file uses mpp extension.
I’ve still tried saving as xml and then opening through project, I’ve attached the screenshot. It does importing correctly, but we really want a proper Mpp file that opens automatically in MsProject and not pure xml file.
Regards,
Peter
Hi Peter,
Thanks for the feedback.
At present, Aspose.Tasks supports saving Project file to XML format only. However, it facilitates you to update existing MPP file. The screenshot you shared is not an error, but in fact, importing XML project file to MS Project.
If you want to perform these steps for MPP file, you can create an empty MPP file using MSP and then update it normally as we are populating the XML file in the above code example. So, in essence, the code goes as follow:
License lic = new License();
lic.SetLicense("Aspose.Total.Product.Family.lic");
ProjectReader reader = new ProjectReader();
Project project = reader.Read("SampleProject.MPP");
//Your code of Tasks Adding/Linking etc. goes here
//Save the project now
ProjectWriter writer = new ProjectWriter();
writer.Write(project, "SampleProject .MPP", TasksDataFormat.MPP);
Please note that we are still in process of implementing R/W facility for different versions of MPP files. You can find a list of the supporting R/W properties of XML and MPP’s by visiting the “MS Project Data Reading and Writing Summary” page.
Thanks a lot,
I’ll try that