Hi,
We added tasks to a blank MPP file by fetching the records from DB and saved the mpp. When I open the updated mpp file and modify any task duration, all the start and end dates become NA. Attached are the two mpp files:
blank.mpp - before updating
TestOutput.mpp - After adding tasks. Change any duration or a date in this file and you will see the dates or duration becoming NA. Below is the code that we are using
public void GetMPPProjectInfo(int assetId, string outputPath)
{
try
{
//create a project instance
License license = new License();
license.SetLicense("Aspose.Total.lic");
ProjectReader pr = new ProjectReader();
Aspose.Tasks.Project prj = pr.Read("C:\\poorna\\Blank.MPP");
Aspose.Tasks.Util.ChildTasksCollector collector1 = new Aspose.Tasks.Util.ChildTasksCollector();
Aspose.Tasks.Util.TaskUtils.Apply(prj.RootTask, collector1, 0);
List lstProjectTasks = GetMPPProjectTasks(assetId);
//project Information
if (prj.RootTask.Calendar == null)
{
prj.RootTask.Calendar = Calendar.Make24HourCalendar();
}
ArrayList projectExtendedAttributes = new ArrayList();
ExtendedAttributeDefinition tastEntityTypeAttr = new ExtendedAttributeDefinition();
tastEntityTypeAttr.Alias = "EntityTypeId";
tastEntityTypeAttr.FieldName = "Number19";
tastEntityTypeAttr.ElementType = ElementType.Task;
tastEntityTypeAttr.CfType = CustomFieldType.Number;
ExtendedAttributeDefinition taskEntityInstanceAttr = new ExtendedAttributeDefinition();
taskEntityInstanceAttr.Alias = "EntityInstanceId";
taskEntityInstanceAttr.FieldName = "Number20";
taskEntityInstanceAttr.ElementType = ElementType.Task;
taskEntityInstanceAttr.CfType = CustomFieldType.Number;
projectExtendedAttributes.Add(tastEntityTypeAttr);
projectExtendedAttributes.Add(taskEntityInstanceAttr);
AddChildrends(prj.RootTask, null, null, 1, assetId, null, null, 22);
//perform recalculations
prj.CalcTaskIds();
prj.CalcTaskUids();
prj.UpdateReferences();
//create a project writer instance
prj.Save(outputPath, Aspose.Tasks.Saving.SaveFileFormat.MPP);
}
catch (Exception ex)
{
throw ex;
}
}
public void AddChildrends(Aspose.Tasks.Task parentTask, int? parentEntityTypeId, int? parentEntityInstanceId, int? entityTypeId, int? entityInstanceId, int? uId, int? id, int? userId)
{
Aspose.Tasks.Task tsk;
int k = 1;
int j = 1;
ArrayList projectExtendedAttributes = new ArrayList();
ExtendedAttributeDefinition taskEntityTypeAttr = new ExtendedAttributeDefinition();
taskEntityTypeAttr.Alias = "EntityTypeId";
taskEntityTypeAttr.FieldName = "Number19";
taskEntityTypeAttr.ElementType = ElementType.Task;
taskEntityTypeAttr.CfType = CustomFieldType.Number;
ExtendedAttributeDefinition taskEntityInstanceAttr = new ExtendedAttributeDefinition();
taskEntityInstanceAttr.Alias = "EntityInstanceId";
taskEntityInstanceAttr.FieldName = "Number20";
taskEntityInstanceAttr.ElementType = ElementType.Task;
taskEntityInstanceAttr.CfType = CustomFieldType.Number;
projectExtendedAttributes.Add(taskEntityTypeAttr);
projectExtendedAttributes.Add(taskEntityInstanceAttr);
ExtendedAttribute taskAttr;
List lstMPPTasks = GetMPPTasksToExport(parentEntityTypeId, parentEntityInstanceId, entityTypeId, entityInstanceId, uId, id, userId);
foreach (ProjectTasksViewModel mv in lstMPPTasks)
{
tsk = new Aspose.Tasks.Task();
//tsk.Uid = k++;
//tsk.Id = j++;
tsk.Name = mv.MPPTaskName;
//tsk.Start = mv.ActualStartDate == null ? DateTime.Now : (DateTime)mv.ActualStartDate;
//tsk.Finish = mv.ActualEndDate == null ? DateTime.Now : (DateTime)mv.ActualEndDate;
//tsk.Duration = TimeSpan.FromDays(mv.Duration == 0 ? 1 : (double)mv.Duration);
tsk.Start = mv.ActualStartDate == null ? DateTime.Now : (DateTime)mv.ActualStartDate;
tsk.Duration = TimeSpan.FromDays(mv.Duration == 0 ? 1 : (double)mv.Duration);
tsk.DurationFormat = TimeUnitType.Day;
tsk.Deadline = tsk.Deadline.AddDays(1 + 1);
tsk.Finish = mv.ActualEndDate == null ? DateTime.Now : (DateTime)mv.ActualEndDate;
tsk.ExtendedAttribute = new ArrayList(1);
taskAttr = new ExtendedAttribute();
taskAttr.AttributeDefinition = taskEntityTypeAttr;
taskAttr.Value = mv.EntityTypeId.ToString();
taskAttr.FieldId = taskEntityTypeAttr.FieldId;
tsk.ExtendedAttribute.Add(taskAttr);
taskAttr = new ExtendedAttribute();
taskAttr.AttributeDefinition = taskEntityInstanceAttr;
taskAttr.Value = mv.EntityInstanceId.ToString();
taskAttr.FieldId = taskEntityInstanceAttr.FieldId;
tsk.ExtendedAttribute.Add(taskAttr);
parentTask.Children.Add(tsk);
AddChildrends(tsk, mv.EntityTypeId, mv.EntityInstanceId, entityTypeId, entityInstanceId, uId, id, userId);
}
}
public void GetMPPProjectInfo(int assetId, string outputPath)
{
try
{
//create a project instance
License license = new License();
license.SetLicense("Aspose.Total.lic");
ProjectReader pr = new ProjectReader();
Aspose.Tasks.Project prj = pr.Read("C:\\poorna\\Blank.MPP");
Aspose.Tasks.Util.ChildTasksCollector collector1 = new Aspose.Tasks.Util.ChildTasksCollector();
Aspose.Tasks.Util.TaskUtils.Apply(prj.RootTask, collector1, 0);
List lstProjectTasks = GetMPPProjectTasks(assetId);
//project Information
if (prj.RootTask.Calendar == null)
{
prj.RootTask.Calendar = Calendar.Make24HourCalendar();
}
ArrayList projectExtendedAttributes = new ArrayList();
ExtendedAttributeDefinition tastEntityTypeAttr = new ExtendedAttributeDefinition();
tastEntityTypeAttr.Alias = "EntityTypeId";
tastEntityTypeAttr.FieldName = "Number19";
tastEntityTypeAttr.ElementType = ElementType.Task;
tastEntityTypeAttr.CfType = CustomFieldType.Number;
ExtendedAttributeDefinition taskEntityInstanceAttr = new ExtendedAttributeDefinition();
taskEntityInstanceAttr.Alias = "EntityInstanceId";
taskEntityInstanceAttr.FieldName = "Number20";
taskEntityInstanceAttr.ElementType = ElementType.Task;
taskEntityInstanceAttr.CfType = CustomFieldType.Number;
projectExtendedAttributes.Add(tastEntityTypeAttr);
projectExtendedAttributes.Add(taskEntityInstanceAttr);
AddChildrends(prj.RootTask, null, null, 1, assetId, null, null, 22);
//perform recalculations
prj.CalcTaskIds();
prj.CalcTaskUids();
prj.UpdateReferences();
//create a project writer instance
prj.Save(outputPath, Aspose.Tasks.Saving.SaveFileFormat.MPP);
}
catch (Exception ex)
{
throw ex;
}
}
public void AddChildrends(Aspose.Tasks.Task parentTask, int? parentEntityTypeId, int? parentEntityInstanceId, int? entityTypeId, int? entityInstanceId, int? uId, int? id, int? userId)
{
Aspose.Tasks.Task tsk;
int k = 1;
int j = 1;
ArrayList projectExtendedAttributes = new ArrayList();
ExtendedAttributeDefinition taskEntityTypeAttr = new ExtendedAttributeDefinition();
taskEntityTypeAttr.Alias = "EntityTypeId";
taskEntityTypeAttr.FieldName = "Number19";
taskEntityTypeAttr.ElementType = ElementType.Task;
taskEntityTypeAttr.CfType = CustomFieldType.Number;
ExtendedAttributeDefinition taskEntityInstanceAttr = new ExtendedAttributeDefinition();
taskEntityInstanceAttr.Alias = "EntityInstanceId";
taskEntityInstanceAttr.FieldName = "Number20";
taskEntityInstanceAttr.ElementType = ElementType.Task;
taskEntityInstanceAttr.CfType = CustomFieldType.Number;
projectExtendedAttributes.Add(taskEntityTypeAttr);
projectExtendedAttributes.Add(taskEntityInstanceAttr);
ExtendedAttribute taskAttr;
List lstMPPTasks = GetMPPTasksToExport(parentEntityTypeId, parentEntityInstanceId, entityTypeId, entityInstanceId, uId, id, userId);
foreach (ProjectTasksViewModel mv in lstMPPTasks)
{
tsk = new Aspose.Tasks.Task();
//tsk.Uid = k++;
//tsk.Id = j++;
tsk.Name = mv.MPPTaskName;
//tsk.Start = mv.ActualStartDate == null ? DateTime.Now : (DateTime)mv.ActualStartDate;
//tsk.Finish = mv.ActualEndDate == null ? DateTime.Now : (DateTime)mv.ActualEndDate;
//tsk.Duration = TimeSpan.FromDays(mv.Duration == 0 ? 1 : (double)mv.Duration);
tsk.Start = mv.ActualStartDate == null ? DateTime.Now : (DateTime)mv.ActualStartDate;
tsk.Duration = TimeSpan.FromDays(mv.Duration == 0 ? 1 : (double)mv.Duration);
tsk.DurationFormat = TimeUnitType.Day;
tsk.Deadline = tsk.Deadline.AddDays(1 + 1);
tsk.Finish = mv.ActualEndDate == null ? DateTime.Now : (DateTime)mv.ActualEndDate;
tsk.ExtendedAttribute = new ArrayList(1);
taskAttr = new ExtendedAttribute();
taskAttr.AttributeDefinition = taskEntityTypeAttr;
taskAttr.Value = mv.EntityTypeId.ToString();
taskAttr.FieldId = taskEntityTypeAttr.FieldId;
tsk.ExtendedAttribute.Add(taskAttr);
taskAttr = new ExtendedAttribute();
taskAttr.AttributeDefinition = taskEntityInstanceAttr;
taskAttr.Value = mv.EntityInstanceId.ToString();
taskAttr.FieldId = taskEntityInstanceAttr.FieldId;
tsk.ExtendedAttribute.Add(taskAttr);
parentTask.Children.Add(tsk);
AddChildrends(tsk, mv.EntityTypeId, mv.EntityInstanceId, entityTypeId, entityInstanceId, uId, id, userId);
}
}