Custom fields in tasks

I am trying to customize a few fields in the task sheet. Following is the code that I have written:


ExtendedAttribute billable = new ExtendedAttribute();
billable.FieldId = ((int)ExtendedAttributeTask.Flag1).ToString();
billable.Value = “Billable”;
TableField billablefld = new TableField();
billablefld.Field = Field.TaskFlag1;
billablefld.Title = “Billable”;

The customized column is not there in the created mpx. Can you pls guide.

Hi Purnima,

Thank you for writing to us again.

Extended attribute can be added to a project by following the steps below:

  1. Create Extended Attribute Definition
  2. Add this definition to project ExtendedAttributes collection
  3. Create extended attribute and set value
  4. Add this extended attribute value to task

Following code works fine for XML, but am afraid that this raises an exception while saving it as MPX. This issue is logged under Id: TASKSNET-1592 for further investigation by the product team. You will be automatically notified once any update is received in this regard.

ExtendedAttributeDefinition
taskExtendedAttributeText9Definition;
#region Create the Extended Attribute Text 9 Definition
taskExtendedAttributeText9Definition = new ExtendedAttributeDefinition();
taskExtendedAttributeText9Definition.Alias = "Task City Name";
taskExtendedAttributeText9Definition.FieldName = "Text9";
taskExtendedAttributeText9Definition.ElementType = ElementType.Task;
taskExtendedAttributeText9Definition.CfType = CustomFieldType.Text;
taskExtendedAttributeText9Definition.FieldId = Convert.ToInt32(ExtendedAttributeTask.Text9).ToString(CultureInfo.InvariantCulture);
#endregion
ExtendedAttributeDefinition taskExtendedAttributeFlag1Definition;
#region Create the Extended Attribute Flag 1 Definition
taskExtendedAttributeFlag1Definition = new ExtendedAttributeDefinition();
taskExtendedAttributeFlag1Definition.Alias = "Is Billable";
taskExtendedAttributeFlag1Definition.FieldName = "Text9";
taskExtendedAttributeFlag1Definition.ElementType = ElementType.Task;
taskExtendedAttributeFlag1Definition.CfType = CustomFieldType.Flag;
taskExtendedAttributeFlag1Definition.FieldId = Convert.ToInt32(ExtendedAttributeTask.Flag1).ToString(CultureInfo.InvariantCulture);
#endregion
Project proj = new Project();
proj.ExtendedAttributes.Add(taskExtendedAttributeText9Definition);
proj.ExtendedAttributes.Add(taskExtendedAttributeFlag1Definition);
Task task = proj.RootTask.Children.Add("Task 1");
task.Set(Tsk.Start, new DateTime(2016, 7, 27, 8, 0, 0));
task.Set(Tsk.Duration, proj.GetDuration(8, TimeUnitType.Hour));
#region Set extended attribute value for task
ExtendedAttribute taskExtendedAttributeText9 = new ExtendedAttribute();
taskExtendedAttributeText9.FieldId = taskExtendedAttributeText9Definition.FieldId;
taskExtendedAttributeText9.Value = "London";
#endregion
task.ExtendedAttributes.Add(taskExtendedAttributeText9);
#region Set extended attribute value for task
ExtendedAttribute taskExtendedAttributeFlag1 = new ExtendedAttribute();
taskExtendedAttributeFlag1.FieldId = taskExtendedAttributeFlag1Definition.FieldId;
taskExtendedAttributeFlag1.Value = "1";
#endregion
task.ExtendedAttributes.Add(taskExtendedAttributeFlag1);
proj.Save(path + "output.xml", SaveFileFormat.XML);
//proj.Save(path + "output.mpx", SaveFileFormat.MPX);//Exception here

Hi Purnima,

As a workaround you may please open the Project object using some template MPP file (attached here for your reference) and then save the MPP file before saving the MPX file. It will create the MPX file along with the extended attributes and their values. Please give it a try and let us know the feedback.

ExtendedAttributeDefinition taskExtendedAttributeText9Definition;
#region Create the Extended Attribute Text 9 Definition
taskExtendedAttributeText9Definition = new ExtendedAttributeDefinition();
taskExtendedAttributeText9Definition.Alias = "Task City Name";
taskExtendedAttributeText9Definition.FieldName = "Text9";
taskExtendedAttributeText9Definition.ElementType = ElementType.Task;
taskExtendedAttributeText9Definition.CfType = CustomFieldType.Text;
taskExtendedAttributeText9Definition.FieldId = Convert.ToInt32(ExtendedAttributeTask.Text9).ToString(CultureInfo.InvariantCulture);
#endregion
ExtendedAttributeDefinition taskExtendedAttributeFlag1Definition;
#region Create the Extended Attribute Flag 1 Definition
taskExtendedAttributeFlag1Definition = new ExtendedAttributeDefinition();
taskExtendedAttributeFlag1Definition.Alias = "Is Billable";
taskExtendedAttributeFlag1Definition.FieldName = "Flag1";
taskExtendedAttributeFlag1Definition.ElementType = ElementType.Task;
taskExtendedAttributeFlag1Definition.CfType = CustomFieldType.Flag;
taskExtendedAttributeFlag1Definition.FieldId = Convert.ToInt32(ExtendedAttributeTask.Flag1).ToString(CultureInfo.InvariantCulture);
#endregion
Project proj = new Project(@"Blank2010.mpp");
proj.ExtendedAttributes.Add(taskExtendedAttributeText9Definition);
proj.ExtendedAttributes.Add(taskExtendedAttributeFlag1Definition);
Task task = proj.RootTask.Children.Add("Task 1");
task.Set(Tsk.Start, new DateTime(2016, 7, 27, 8, 0, 0));
task.Set(Tsk.Duration, proj.GetDuration(8, TimeUnitType.Hour));
#region Set extended attribute value for task
ExtendedAttribute taskExtendedAttributeText9 = new ExtendedAttribute();
taskExtendedAttributeText9.FieldId = taskExtendedAttributeText9Definition.FieldId;
taskExtendedAttributeText9.Value = "London";
#endregion
task.ExtendedAttributes.Add(taskExtendedAttributeText9);
#region Set extended attribute value for task
ExtendedAttribute taskExtendedAttributeFlag1 = new ExtendedAttribute();
taskExtendedAttributeFlag1.FieldId = taskExtendedAttributeFlag1Definition.FieldId;
taskExtendedAttributeFlag1.Value = "1";
#endregion
task.ExtendedAttributes.Add(taskExtendedAttributeFlag1);
proj.Save("output2.mpp", SaveFileFormat.MPP);//Additional workaround step
proj.Save("output2.mpx", SaveFileFormat.MPX);

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


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