Writing Metadata to MPP
Contents
[
Hide
Show
]Aspose.Tasks for .NET 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 MPP files:
- 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.
1Project project = new Project("New Project.mpp");
2
3// Add working times to project calendar
4WorkingTime wt = new WorkingTime();
5wt.FromTime = new DateTime(2010, 1, 1, 19, 0, 0);
6wt.ToTime = new DateTime(2010, 1, 1, 20, 0, 0);
7WeekDay day = project.Get(Prj.Calendar).WeekDays.ToList()[1];
8day.WorkingTimes.Add(wt);
9
10// Change calendar name
11project.Get(Prj.Calendar).Name = "CHANGED NAME!";
12
13// Add tasks and set task meta data
14Task task = project.RootTask.Children.Add("Task 1");
15task.Set(Tsk.DurationFormat, TimeUnitType.Day);
16task.Set(Tsk.Duration, project.GetDuration(3));
17task.Set(Tsk.Contact, "Rsc 1");
18task.Set(Tsk.IsMarked, true);
19task.Set(Tsk.IgnoreWarnings, true);
20Task task2 = project.RootTask.Children.Add("Task 2");
21task2.Set(Tsk.DurationFormat, TimeUnitType.Day);
22task2.Set(Tsk.Contact, "Rsc 2");
23
24// Link tasks
25project.TaskLinks.Add(task, task2, TaskLinkType.FinishToStart, project.GetDuration(-1, TimeUnitType.Day));
26
27// Set project start date
28project.Set(Prj.StartDate, new DateTime(2013, 8, 13, 9, 0, 0));
29
30// Add resource and set resource meta data
31Resource resource = project.Resources.Add("Rsc 1");
32resource.Set(Rsc.Type, ResourceType.Work);
33resource.Set(Rsc.Initials, "WR");
34resource.Set(Rsc.AccrueAt, CostAccrualType.Prorated);
35resource.Set(Rsc.MaxUnits, 1);
36resource.Set(Rsc.Code, "Code 1");
37resource.Set(Rsc.Group, "Workers");
38resource.Set(Rsc.EMailAddress, "1@gmail.com");
39resource.Set(Rsc.WindowsUserAccount, "user_acc1");
40resource.Set(Rsc.IsGeneric, new NullableBool(true));
41resource.Set(Rsc.AccrueAt, CostAccrualType.End);
42resource.Set(Rsc.StandardRate, 10);
43resource.Set(Rsc.StandardRateFormat, RateFormatType.Day);
44resource.Set(Rsc.OvertimeRate, 15);
45resource.Set(Rsc.OvertimeRateFormat, RateFormatType.Hour);
46resource.Set(Rsc.IsTeamAssignmentPool, true);
47resource.Set(Rsc.CostCenter, "Cost Center 1");
48
49// Create resource assignment and set resource assignment meta data
50ResourceAssignment assignment = project.ResourceAssignments.Add(task, resource);
51assignment.Set(Asn.Uid, 1);
52assignment.Set(Asn.Work, task.Get(Tsk.Duration));
53assignment.Set(Asn.RemainingWork, assignment.Get(Asn.Work));
54assignment.Set(Asn.RegularWork, assignment.Get(Asn.Work));
55task.Set(Tsk.Work, assignment.Get(Asn.Work));
56
57resource.Set(Rsc.Work, task.Get(Tsk.Work));
58assignment.Set(Asn.Start, task.Get(Tsk.Start));
59assignment.Set(Asn.Finish, task.Get(Tsk.Finish));
60
61// Add extended attribute for project and task
62ExtendedAttributeDefinition attr = ExtendedAttributeDefinition.CreateTaskDefinition(CustomFieldType.Flag, ExtendedAttributeTask.Flag1, "My Flag Field");
63project.ExtendedAttributes.Add(attr);
64
65ExtendedAttribute taskAttr = attr.CreateExtendedAttribute();
66taskAttr.FlagValue = true;
67task2.ExtendedAttributes.Add(taskAttr);
68
69project.Save("WriteMetaData_out.mpp", SaveFileFormat.MPP);