Working with Extended Task Attributes

In Microsoft Project, an extended task attribute is an element used to capture data for a custom task field. Aspose.Tasks can create as well as 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:

  1. Create ExtendedAttributeDefinition for the Attribute with necessary parameters
  2. Create ExtendedAttribute from the definition in the previous step
  3. Add the created ExtendedAttribute to the task
  1// The path to the documents directory.
  2System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
  3        
  4// Create new project
  5System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"Blank2010.mpp");
  6        
  7        
  8// C# preprocessor directive: #region Adding Plain Text Attribute
  9        
 10//Create an Extended Attribute Definition of Text1 type
 11auto taskExtendedAttributeText1Definition = ExtendedAttributeDefinition::CreateTaskDefinition(Aspose::Tasks::CustomFieldType::Text, Aspose::Tasks::ExtendedAttributeTask::Text1, u"Task City Name");
 12        
 13//Add it to the project's Extended Attributes collection
 14project->get_ExtendedAttributes()->Add(taskExtendedAttributeText1Definition);
 15        
 16//Add a task to the project
 17auto task = project->get_RootTask()->get_Children()->Add(u"Task 1");
 18        
 19//Create an Extended Attribute from the Attribute Definition
 20auto taskExtendedAttributeText1 = taskExtendedAttributeText1Definition->CreateExtendedAttribute();
 21        
 22//Assign a value to the generated Extended Attribute. The type of the attribute is "Text", the "TextValue" property should be used.
 23taskExtendedAttributeText1->set_TextValue(u"London");
 24        
 25//Add the Extended Attribute to task
 26task->get_ExtendedAttributes()->Add(taskExtendedAttributeText1);
 27        
 28project->Save(dataDir + u"PlainTextExtendedAttribute_out.mpp", Aspose::Tasks::Saving::SaveFileFormat::MPP);
 29        
 30// C# preprocessor directive: #endregion
 31        
 32        
 33        
 34// C# preprocessor directive: #region Adding Text Attribute with Lookup option
 35        
 36        
 37System::SharedPtr<Project> project1 = System::MakeObject<Project>(dataDir + u"Blank2010.mpp");
 38        
 39//Create an Extended Attribute Definition of Text2 type
 40auto taskExtendedAttributeText2Definition = ExtendedAttributeDefinition::CreateLookupTaskDefinition(Aspose::Tasks::CustomFieldType::Text, Aspose::Tasks::ExtendedAttributeTask::Text2, u"Task Towns Name");
 41        
 42//Add lookup values for the extended attribute definition
 43taskExtendedAttributeText2Definition->AddLookupValue([&]{ auto tmp_0 = System::MakeObject<Value>(); tmp_0->set_Id(1); tmp_0->set_StringValue(u"Town1"); tmp_0->set_Description(u"This is Town1"); return tmp_0; }());
 44taskExtendedAttributeText2Definition->AddLookupValue([&]{ auto tmp_1 = System::MakeObject<Value>(); tmp_1->set_Id(2); tmp_1->set_StringValue(u"Town2"); tmp_1->set_Description(u"This is Town2"); return tmp_1; }());
 45        
 46//Add it to the porject's Extended Attributes collection
 47project1->get_ExtendedAttributes()->Add(taskExtendedAttributeText2Definition);
 48        
 49//Add a task to the project
 50auto task2 = project1->get_RootTask()->get_Children()->Add(u"Task 2");
 51        
 52//Crate an Extended Attribute from the Text2 Lookup Definition for Id 1
 53auto taskExtendedAttributeText2 = taskExtendedAttributeText2Definition->CreateExtendedAttribute(taskExtendedAttributeText2Definition->get_ValueList()->idx_get(1));
 54        
 55//Add the Extended Attribute to task
 56task2->get_ExtendedAttributes()->Add(taskExtendedAttributeText2);
 57        
 58project1->Save(dataDir + u"TextExtendedAttributeWithLookup_out.mpp", Aspose::Tasks::Saving::SaveFileFormat::MPP);
 59        
 60// C# preprocessor directive: #endregion
 61        
 62        
 63        
 64// C# preprocessor directive: #region Adding Duration Attribute with Lookup option
 65        
 66        
 67System::SharedPtr<Project> project2 = System::MakeObject<Project>(dataDir + u"Blank2010.mpp");
 68        
 69//Create an Extended Attribute Definition of Duration2 type
 70auto taskExtendedAttributeDuration2Definition = ExtendedAttributeDefinition::CreateLookupTaskDefinition(Aspose::Tasks::CustomFieldType::Duration, Aspose::Tasks::ExtendedAttributeTask::Duration2, u"Some duration");
 71        
 72//Add lookup values for extended attribute definition
 73taskExtendedAttributeDuration2Definition->AddLookupValue([&]{ auto tmp_2 = System::MakeObject<Value>(); tmp_2->set_Id(2); tmp_2->set_Duration(project2->GetDuration(4, Aspose::Tasks::TimeUnitType::Hour)); tmp_2->set_Description(u"4 hours"); return tmp_2; }());
 74taskExtendedAttributeDuration2Definition->AddLookupValue([&]{ auto tmp_3 = System::MakeObject<Value>(); tmp_3->set_Id(3); tmp_3->set_Duration(project2->GetDuration(1, Aspose::Tasks::TimeUnitType::Day)); tmp_3->set_Description(u"1 day"); return tmp_3; }());
 75taskExtendedAttributeDuration2Definition->AddLookupValue([&]{ auto tmp_4 = System::MakeObject<Value>(); tmp_4->set_Id(4); tmp_4->set_Duration(project2->GetDuration(1, Aspose::Tasks::TimeUnitType::Hour)); tmp_4->set_Description(u"1 hour"); return tmp_4; }());
 76taskExtendedAttributeDuration2Definition->AddLookupValue([&]{ auto tmp_5 = System::MakeObject<Value>(); tmp_5->set_Id(7); tmp_5->set_Duration(project2->GetDuration(10, Aspose::Tasks::TimeUnitType::Day)); tmp_5->set_Description(u"10 days"); return tmp_5; }());
 77        
 78//Add the definition to the project's Extended Attributes collection
 79project2->get_ExtendedAttributes()->Add(taskExtendedAttributeDuration2Definition);
 80        
 81//Add a task to the project
 82auto task3 = project2->get_RootTask()->get_Children()->Add(u"Task 3");
 83        
 84//Create an Extended Attribute from the Duration2 Lookup Definition for Id 3
 85auto taskExtendedAttributeDuration2 = taskExtendedAttributeDuration2Definition->CreateExtendedAttribute(taskExtendedAttributeDuration2Definition->get_ValueList()->idx_get(3));
 86        
 87//Add the Extended Attribute to task
 88task3->get_ExtendedAttributes()->Add(taskExtendedAttributeDuration2);
 89        
 90project2->Save(dataDir + u"DurationExtendedAttributeWithLookup_out.mpp", Aspose::Tasks::Saving::SaveFileFormat::MPP);
 91        
 92        
 93// C# preprocessor directive: #endregion
 94        
 95        
 96        
 97// C# preprocessor directive: #region Adding Date-Time with Lookup option
 98        
 99        
100System::SharedPtr<Project> project3 = System::MakeObject<Project>(dataDir + u"Blank2010.mpp");
101        
102//Create an Extended Attribute Definition of Finish2 Type
103auto taskExtendedAttributeFinish2Definition = ExtendedAttributeDefinition::CreateLookupTaskDefinition(Aspose::Tasks::CustomFieldType::Finish, Aspose::Tasks::ExtendedAttributeTask::Finish2, u"Some finish");
104        
105//Add lookup values for extended attribute defintion
106taskExtendedAttributeFinish2Definition->AddLookupValue([&]{ auto tmp_6 = System::MakeObject<Value>(); tmp_6->set_Id(2); tmp_6->set_DateTimeValue(System::DateTime(1984, 1, 1, 00, 00, 1)); tmp_6->set_Description(u"This is Value2"); return tmp_6; }());
107taskExtendedAttributeFinish2Definition->AddLookupValue([&]{ auto tmp_7 = System::MakeObject<Value>(); tmp_7->set_Id(3); tmp_7->set_DateTimeValue(System::DateTime(1994, 1, 1, 00, 1, 1)); tmp_7->set_Description(u"This is Value3"); return tmp_7; }());
108taskExtendedAttributeFinish2Definition->AddLookupValue([&]{ auto tmp_8 = System::MakeObject<Value>(); tmp_8->set_Id(4); tmp_8->set_DateTimeValue(System::DateTime(2009, 12, 31, 00, 00, 00)); tmp_8->set_Description(u"This is Value4"); return tmp_8; }());
109taskExtendedAttributeFinish2Definition->AddLookupValue([&]{ auto tmp_9 = System::MakeObject<Value>(); tmp_9->set_Id(7); tmp_9->set_DateTimeValue(System::DateTime::get_Now()); tmp_9->set_Description(u"This is Value6"); return tmp_9; }());
110        
111//Add the definition to the project's Extended Attributes collection
112project3->get_ExtendedAttributes()->Add(taskExtendedAttributeFinish2Definition);
113        
114//Add a task to the project
115auto task4 = project3->get_RootTask()->get_Children()->Add(u"Task 4");
116        
117//Create an Extneded Attribute from the Finish2 Lookup Definition for Id 3
118auto taskExtendedAttributeFinish2 = taskExtendedAttributeFinish2Definition->CreateExtendedAttribute(taskExtendedAttributeFinish2Definition->get_ValueList()->idx_get(3));
119        
120//Add the Extended Attribute to task
121task4->get_ExtendedAttributes()->Add(taskExtendedAttributeFinish2);
122        
123// Save the Project               
124project3->Save(dataDir + u"FinishExtendedAttributeWithLookup_out.mpp", Aspose::Tasks::Saving::SaveFileFormat::MPP);
125        
126// C# preprocessor directive: #endregion
127        
128        
129        

Reading Extended Task Attributes Information

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 code example demonstrates how to view a task’s extended attributes.

 1// Create project instance
 2System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
 3System::SharedPtr<Project> project1 = System::MakeObject<Project>(dataDir + u"ReadTaskExtendedAttributes.mpp");
 4    
 5// Read extended attributes for tasks
 6    
 7{
 8    auto tsk_enumerator = (project1->get_RootTask()->get_Children())->GetEnumerator();
 9    decltype(tsk_enumerator->get_Current()) tsk;
10    while (tsk_enumerator->MoveNext() && (tsk = tsk_enumerator->get_Current(), true))
11    {
12        auto ea_enumerator = (tsk->get_ExtendedAttributes())->GetEnumerator();
13        decltype(ea_enumerator->get_Current()) ea;
14        while (ea_enumerator->MoveNext() && (ea = ea_enumerator->get_Current(), true))
15        {
16            System::Console::WriteLine(ea->get_FieldId());
17            System::Console::WriteLine(ea->get_ValueGuid());
18            
19            switch (ea->get_AttributeDefinition()->get_CfType())
20            {
21                case Aspose::Tasks::CustomFieldType::Date:
22                case Aspose::Tasks::CustomFieldType::Start:
23                case Aspose::Tasks::CustomFieldType::Finish:
24                    System::Console::WriteLine(System::ObjectExt::Box<System::DateTime>(ea->get_DateValue()));
25                    break;
26                
27                case Aspose::Tasks::CustomFieldType::Text:
28                    System::Console::WriteLine(ea->get_TextValue());
29                    break;
30                
31                case Aspose::Tasks::CustomFieldType::Duration:
32                    System::Console::WriteLine(System::ObjectExt::ToString(ea->get_DurationValue()));
33                    break;
34                
35                case Aspose::Tasks::CustomFieldType::Cost:
36                case Aspose::Tasks::CustomFieldType::Number:
37                    System::Console::WriteLine(ea->get_NumericValue());
38                    break;
39                
40                case Aspose::Tasks::CustomFieldType::Flag:
41                    System::Console::WriteLine(ea->get_FlagValue());
42                    break;
43                
44                default:
45                    break;
46            }
47        }
48    }
49}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.