Hi,
Do you have sample code of setting task’s extended attribute in Java?
I am trying to use the follwing code but got exception when trying to save to MPP format:
final ExtendedAttribute ea = new ExtendedAttribute();
ea.setFieldId(“1”);
ea.setValue(fieldValue);
task.getExtendedAttribute().add(ea);
Thanks!
Hi Ying,
//call read method of project reader object to get project object
Project prj = new Project(“d:\aspose\Project5.mpp”);
List eads = prj.getExtendedAttributes();
if (eads == null)
{
prj.setExtendedAttributes(eads);
}
// Create extended attribute definition
ExtendedAttributeDefinition ead = new ExtendedAttributeDefinition();
StringBuilder sb = new StringBuilder();
sb.append(ExtendedAttributeTask.Start7);
String strI = sb.toString();
ead.setFieldId(strI);
ead.setFieldName(“Start7”);
eads.add(ead);
// Get zero index task
Task tsk = (Task) prj.getRootTask().getChildren().get(0);
List eas = tsk.getExtendedAttribute();
if (eas == null)
{
tsk.setExtendedAttribute(eas);
}
//String dateTimeFormat = “yyyy-MM-ddTHH:mm:ss”;
ExtendedAttribute ea = new ExtendedAttribute();
ea.setFieldId(ead.getFieldId());
System.out.println(java.util.Calendar.getInstance().getTime().toString());
//ea.setValue(java.util.Calendar.getInstance().getTime().toString());
ea.setValue(“2013-12-05 09:00:00”);
eas.add(ea);
prj.save(“D:\Aspose\Project5.xml”, SaveFileFormat.XML);
Thanks for providing the sample codes. However, the code does not work for me. I have copied your code into my standalone java project in function addProjectExtendedAtrributes(). However, I did not see the attribute is added to the column.
Please use the attached zip to verify and provide a workable solution for it.
Thank you!
Hi Ying,
//add extended attribute definition
ExtendedAttributeDefinition ead = new ExtendedAttributeDefinition();
ead.setFieldName(“Text1”);
ead.setAlias(“Description”);
StringBuilder sb = new StringBuilder();
sb.append("");
sb.append(ExtendedAttributeTask.Text1);
String strI = sb.toString();
ead.setFieldId(strI);
if (prj.getExtendedAttributes() == null)
{
prj.setExtendedAttributes(new ArrayList());
}
prj.getExtendedAttributes().add(ead);
for (Task task : prj.getRootTask().getChildren())
{
ExtendedAttribute ea = new ExtendedAttribute();
ea.setFieldId(ead.getFieldId());
ea.setValue("Description of " + task.getName());
task.getExtendedAttribute().add(ea);
}
prj.save(“D:/Aspose/project1Java_res.mpp”, SaveFileFormat.MPP);
Hi Ying,
//add extended attribute definition
ExtendedAttributeDefinition ead = new ExtendedAttributeDefinition();
ead.setFieldName(“Text1”);
ead.setAlias(“Description”);
StringBuilder sb = new StringBuilder();
sb.append("");
sb.append(ExtendedAttributeTask.Text1);
String strI = sb.toString();
ead.setFieldId(strI);
if (prj.getExtendedAttributes() == null)
{
prj.setExtendedAttributes(new ArrayList());
}
prj.getExtendedAttributes().add(ead);
for (Task task : prj.getRootTask().getChildren())
{
ExtendedAttribute ea = new ExtendedAttribute();
ea.setFieldId(ead.getFieldId());
ea.setValue("Description of " + task.getName());
task.getExtendedAttribute().add(ea);
}
prj.save(“D:/Aspose/project1Java_res.mpp”, SaveFileFormat.MPP);
Can you send me the full Java standalone project that worked for you?
I could not make it work by using your code and the template.
I have attached my code with the section of adding extended attribute from your example here. Can you give it a try from your end?
Hi Ying,
Thanks for the sample project.
I see this field shows in the task’s “optional fields” tab, but not as a column.
Is this the designed behavior?
Hi Ying,
The extended attribute fields are not shown by default and needs to be added to the project using the “Add New Column” option in Microsoft Project. Please feel free to write to us in case you have any other query/inquiry in this regard.
Thanks for the clarification. It works.