Sample code of setting tasks's extended attribute in Java

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,


Thank you for contacting Aspose support team.

Following is a sample code which can be used to create an extended attribute for a task. It is assumed that the template project “Project5.mpp” contains at least one task to run this sample code. Could you please give it a try and let us know your feedback?

//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,


Here is another sample code which can be used to add extended attribute. Could you please give it a try and let us know your feedback? The input template file is attached here for your reference.

Project prj = new Project(“D:/Aspose/Project1.mpp”);

//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);

P.S. I have checked the output of the previous code and observed that if we add the column, the user defined date is present there.

Hi Ying,


Here is another sample code which can be used to add extended attribute. Could you please give it a try and let us know your feedback? The input template file is attached here for your reference.

Project prj = new Project(“D:/Aspose/Project1.mpp”);

//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);

P.S. I have checked the output of the previous code and observed that if we add the column, the user defined date is present there.

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,


Here the complete project is attached which I used for testing.

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.