Working with Extended Task Attributes
Contents
[
Hide
Show
]In Microsoft Project, an extended task attribute is an element used to capture data for a custom task field. Aspose.Tasks can retrieve extended attribute information for all versions of Microsoft Project: 2003, 2007, 2010, and 2013.
Adding Extended Attribute Information for a Task
Creating an Extended attribute requires the following steps:
- Create ExtendedAttributeDefinition for the Attribute with necessary parameters
- Create ExtendedAttribute from the definition in the previous step
- Add the created ExtendedAttribute to the task
1// The path to the documents directory.
2String dataDir = Utils.getDataDir(AddTaskExtendedAttributes.class);
3
4// Create new project
5Project project = new Project(dataDir + "project.mpp");
6
7//1. Adding Plain Text Attribute
8
9//Create an Extended Attribute Definition of Text1 type
10ExtendedAttributeDefinition taskExtendedAttributeText1Definition = ExtendedAttributeDefinition.createTaskDefinition(CustomFieldType.Text, ExtendedAttributeTask.Text1, "Task City Name");
11
12//Add it to the project's Extended Attributes collection
13project.getExtendedAttributes().add(taskExtendedAttributeText1Definition);
14
15//Add a task to the project
16Task task = project.getRootTask().getChildren().add("Task 1");
17
18//Create an Extended Attribute from the Attribute Definition
19ExtendedAttribute taskExtendedAttributeText1 = taskExtendedAttributeText1Definition.createExtendedAttribute();
20
21//Assign a value to the generated Extended Attribute
22taskExtendedAttributeText1.setTextValue("London");
23
24//Add the Extended Attribute to task
25task.getExtendedAttributes().add(taskExtendedAttributeText1);
26
27project.save(dataDir + "PlainTextExtendedAttribute_out.mpp", SaveFileFormat.MPP);
28
29//2. Adding Text Attribute with Lookup option
30Project project1 = new Project(dataDir + "project.mpp");
31
32//Create an Extended Attribute Definition of Text2 type
33ExtendedAttributeDefinition taskExtendedAttributeText2Definition = ExtendedAttributeDefinition.createLookupTaskDefinition(CustomFieldType.Text, ExtendedAttributeTask.Text2, "Task Towns Name");
34
35//Add lookup values for the extended attribute definition
36Value val = new Value();
37val.setId(1);
38val.setStringValue("Town1");
39val.setDescription("This is Town1");
40taskExtendedAttributeText2Definition.addLookupValue(val);
41
42val = new Value();
43val.setId(2);
44val.setStringValue("Town2");
45val.setDescription("This is Town2");
46taskExtendedAttributeText2Definition.addLookupValue(val);
47
48//Add it to the porject's Extended Attributes collection
49project1.getExtendedAttributes().add(taskExtendedAttributeText2Definition);
50
51//Add a task to the project
52Task task2 = project1.getRootTask().getChildren().add("Task 2");
53
54//Crate an Extended Attribute from the Text2 Lookup Definition for Id 1
55ExtendedAttribute taskExtendedAttributeText2 = taskExtendedAttributeText2Definition.createExtendedAttribute(taskExtendedAttributeText2Definition.getValueList().get(1));
56
57//Add the Extended Attribute to task
58task2.getExtendedAttributes().add(taskExtendedAttributeText2);
59
60project1.save(dataDir + "TextExtendedAttributeWithLookup_out.mpp", SaveFileFormat.MPP);
61
62//3. Adding Duration Attribute with Lookup option
63
64Project project2 = new Project(dataDir + "project.mpp");
65
66//Create an Extended Attribute Definition of Duration2 type
67ExtendedAttributeDefinition taskExtendedAttributeDuration2Definition = ExtendedAttributeDefinition.createLookupTaskDefinition(CustomFieldType.Duration, ExtendedAttributeTask.Duration2, "Some duration");
68
69//Add lookup values for extended attribute definition
70val = new Value();
71val.setId(2);
72val.setDuration(project2.getDuration(4, TimeUnitType.Hour));
73val.setDescription("4 hours");
74taskExtendedAttributeDuration2Definition.addLookupValue(val);
75
76
77val = new Value();
78val.setId(3);
79val.setDuration(project2.getDuration(8, TimeUnitType.Day));
80val.setDescription("1 day");
81taskExtendedAttributeDuration2Definition.addLookupValue(val);
82
83val = new Value();
84val.setId(4);
85val.setDuration(project2.getDuration(1, TimeUnitType.Hour));
86val.setDescription("1 hour");
87taskExtendedAttributeDuration2Definition.addLookupValue(val);
88
89val = new Value();
90val.setId(7);
91val.setDuration(project2.getDuration(10, TimeUnitType.Day));
92val.setDescription("10 days");
93taskExtendedAttributeDuration2Definition.addLookupValue(val);
94
95//Add the definition to the project's Extended Attributes collection
96project2.getExtendedAttributes().add(taskExtendedAttributeDuration2Definition);
97
98//Add a task to the project
99Task task3 = project2.getRootTask().getChildren().add("Task 3");
100
101//Create an Extended Attribute from the Duration2 Lookup Definition for Id 3
102ExtendedAttribute taskExtendedAttributeDuration2 = taskExtendedAttributeDuration2Definition.createExtendedAttribute(taskExtendedAttributeDuration2Definition.getValueList().get(3));
103
104//Add the Extended Attribute to task
105task3.getExtendedAttributes().add(taskExtendedAttributeDuration2);
106
107project2.save(dataDir + "DurationExtendedAttributeWithLookup_out.mpp", SaveFileFormat.MPP);
Reading Extended Task Attributes
The ExtendedAttribute property exposed by the Task class is used to manage a task’s extended attributes. This property reads and writes an ArrayList of ExtendedAttribute objects to deal with a task’s extended attributes. The ExtendedAttribute object further exposes the relevant properties.
The following example shows how to view a task’s extended attributes with Aspose.Tasks.
1// The path to the documents directory.
2String dataDir = Utils.getDataDir(ExtendedTaskAttributes.class);
3
4Project project = new Project(dataDir + "Input.mpp");
5
6for (Task tsk:project.getRootTask().getChildren())
7{
8 for (ExtendedAttribute ea:tsk.getExtendedAttributes())
9 {
10 System.out.println(ea.getFieldId());
11 System.out.println(ea.getValueGuid());
12
13 switch (ea.getAttributeDefinition().getCfType())
14 {
15 case CustomFieldType.Date:
16 case CustomFieldType.Start:
17 case CustomFieldType.Finish:
18 System.out.println(ea.getDateValue());
19 break;
20
21 case CustomFieldType.Text:
22 System.out.println(ea.getTextValue());
23 break;
24
25 case CustomFieldType.Duration:
26 System.out.println(ea.getDurationValue().toString());
27 break;
28
29 case CustomFieldType.Cost:
30 case CustomFieldType.Number:
31 System.out.println(ea.getNumericValue());
32 break;
33
34 case CustomFieldType.Flag:
35 System.out.println(ea.getFlagValue());
36 break;
37 }
38 }
39}