Writing Metadata to MPP
Contents
[
Hide
Show
]Aspose.Tasks for C++ API provides a facility for writing metadata to MPP files for calendars, tasks, task links, resources and resource assignments.
Writing Metadata to Microsoft Project Files
The following metadata is used in this example:
- Calendar
- CalendarName
- Task
- Contact
- IsMarked
- IgnoreWarnings
- TaskLink
- LagFormat
- LinkLag
- Resource
- Type
- Initials
- MaxUnits
- Code
- Group
- EmailAddress
- NTAccount
- IsGeneric
- AccrueAt
- TeamAssignmentPool
- CostCenter
- ResourceAssignment
- Uid
- Work
- RemainingWork
- RegularWork
- Start
- Finish
Here IsMarked and IgnoreWarnings are newly added fields for a task. Similarly for resources, the TeamAssignmentPool and CostCenter properties are provided.
The following steps are followed in the sample code to demonstrate how to write metadata to MPPs:
- Open a new, blank MPP file.
- Add working times on Monday of the project calendar and change the project calendar name.
- Add three tasks with task links and lags.
- Add two resources, one budget work and one budget cost resource (four in total).
- Assign budget resources to the project root task (the project summary task).
- Assign another two resources to the first and second tasks.
- Set the project baseline.
- Add a new task extended attribute.
- Save changes as an MPP file.
1System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
2System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"Project1.mpp");
3
4// Add working times to project calendar
5System::SharedPtr<WorkingTime> wt = System::MakeObject<WorkingTime>();
6wt->set_FromTime(System::DateTime(2010, 1, 1, 19, 0, 0));
7wt->set_ToTime(System::DateTime(2010, 1, 1, 20, 0, 0));
8System::SharedPtr<WeekDay> day = project->Get<System::SharedPtr<Calendar>>(Prj::Calendar())->get_WeekDays()->ToList()->idx_get(1);
9day->get_WorkingTimes()->Add(wt);
10
11// Change calendar name
12project->Get<System::SharedPtr<Calendar>>(Prj::Calendar())->set_Name(u"CHANGED NAME!");
13
14// Add tasks and set task meta data
15System::SharedPtr<Task> task = project->get_RootTask()->get_Children()->Add(u"Task 1");
16task->Set<TimeUnitType>(Tsk::DurationFormat(), Aspose::Tasks::TimeUnitType::Day);
17task->Set<Duration>(Tsk::Duration(), project->GetDuration(3));
18task->Set<System::String>(Tsk::Contact(), u"Rsc 1");
19task->Set<bool>(Tsk::IsMarked(), true);
20task->Set<bool>(Tsk::IgnoreWarnings(), true);
21System::SharedPtr<Task> task2 = project->get_RootTask()->get_Children()->Add(u"Task 2");
22task2->Set<TimeUnitType>(Tsk::DurationFormat(), Aspose::Tasks::TimeUnitType::Day);
23task2->Set<System::String>(Tsk::Contact(), u"Rsc 2");
24
25// Link tasks
26project->get_TaskLinks()->Add(task, task2, Aspose::Tasks::TaskLinkType::FinishToStart, project->GetDuration(-1, Aspose::Tasks::TimeUnitType::Day));
27
28// Set project start date
29project->Set(Prj::StartDate(), System::DateTime(2013, 8, 13, 9, 0, 0));
30
31// Add resource and set resource meta data
32System::SharedPtr<Resource> rsc1 = project->get_Resources()->Add(u"Rsc 1");
33rsc1->Set<ResourceType>(Rsc::Type(), Aspose::Tasks::ResourceType::Work);
34rsc1->Set<System::String>(Rsc::Initials(), u"WR");
35rsc1->Set<CostAccrualType>(Rsc::AccrueAt(), Aspose::Tasks::CostAccrualType::Prorated);
36rsc1->Set<double>(Rsc::MaxUnits(), 1.0);
37rsc1->Set<System::String>(Rsc::Code(), u"Code 1");
38rsc1->Set<System::String>(Rsc::Group(), u"Workers");
39rsc1->Set<System::String>(Rsc::EMailAddress(), u"1@gmail.com");
40rsc1->Set<System::String>(Rsc::WindowsUserAccount(), u"user_acc1");
41rsc1->Set<NullableBool>(Rsc::IsGeneric(), NullableBool(true));
42rsc1->Set<CostAccrualType>(Rsc::AccrueAt(), Aspose::Tasks::CostAccrualType::End);
43rsc1->Set<System::Decimal>(Rsc::StandardRate(), static_cast<System::Decimal>(10));
44rsc1->Set<RateFormatType>(Rsc::StandardRateFormat(), Aspose::Tasks::RateFormatType::Day);
45rsc1->Set<System::Decimal>(Rsc::OvertimeRate(), static_cast<System::Decimal>(15));
46rsc1->Set<RateFormatType>(Rsc::OvertimeRateFormat(), Aspose::Tasks::RateFormatType::Hour);
47
48rsc1->Set<bool>(Rsc::IsTeamAssignmentPool(), true);
49rsc1->Set<System::String>(Rsc::CostCenter(), u"Cost Center 1");
50
51// Create resource assignment and set resource assignment meta data
52System::SharedPtr<ResourceAssignment> assn = project->get_ResourceAssignments()->Add(task, rsc1);
53assn->Set<int32_t>(Asn::Uid(), 1);
54assn->Set<Duration>(Asn::Work(), task->Get<Duration>(Tsk::Duration()));
55assn->Set<Duration>(Asn::RemainingWork(), assn->Get<Duration>(Asn::Work()));
56assn->Set<Duration>(Asn::RegularWork(), assn->Get<Duration>(Asn::Work()));
57task->Set<Duration>(Tsk::Work(), assn->Get<Duration>(Asn::Work()));
58
59rsc1->Set<Duration>(Rsc::Work(), task->Get<Duration>(Tsk::Work()));
60assn->Set(Asn::Start(), task->Get<System::DateTime>(Tsk::Start()));
61assn->Set(Asn::Finish(), task->Get<System::DateTime>(Tsk::Finish()));
62
63// Add extended attribute for project and task
64System::SharedPtr<ExtendedAttributeDefinition> attr = ExtendedAttributeDefinition::CreateTaskDefinition(Aspose::Tasks::CustomFieldType::Flag, Aspose::Tasks::ExtendedAttributeTask::Flag1, u"My Flag Field");
65project->get_ExtendedAttributes()->Add(attr);
66
67System::SharedPtr<ExtendedAttribute> taskAttr = attr->CreateExtendedAttribute();
68taskAttr->set_FlagValue(true);
69task2->get_ExtendedAttributes()->Add(taskAttr);
70
71// Save project as MPP
72project->Save(dataDir + u"WriteMetaData_out.mpp", Aspose::Tasks::Saving::SaveFileFormat::MPP);