public class Creator
{
private readonly Project asposeProject;
public Creator(Project asposeProject)
{
this.asposeProject = asposeProject;
}
public void Run()
{
ExtendedAttributeDefinition myTextAttributeDefinition = asposeProject.ExtendedAttributes.GetById((int)ExtendedAttributeTask.Text1);
// If the Custom field doesn't exist in Project, create it
if (myTextAttributeDefinition == null)
{
myTextAttributeDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Text1, "My text field");
asposeProject.ExtendedAttributes.Add(myTextAttributeDefinition);
}
// Generate Extended Attribute from definition
ExtendedAttribute text1TaskAttribute = myTextAttributeDefinition.CreateExtendedAttribute();
text1TaskAttribute.TextValue = "Text attribute value";
// Add extended attribute to task
Task task = asposeProject.RootTask.Children.Add("Task 1");
task.ExtendedAttributes.Add(text1TaskAttribute);
// Define a duration attribute without lookup.
ExtendedAttributeDefinition taskDurationAttributeDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Duration1, "New Duration");
asposeProject.ExtendedAttributes.Add(taskDurationAttributeDefinition);
// Add new task and assign duration value to the previously defined duration attribute.
ExtendedAttribute durationExtendedAttribute = taskDurationAttributeDefinition.CreateExtendedAttribute();
durationExtendedAttribute.DurationValue = asposeProject.GetDuration(3.0, TimeUnitType.ElapsedDay);
task.ExtendedAttributes.Add(durationExtendedAttribute);
Task task2 = asposeProject.RootTask.Children.Add("Task 2");
// Generate Extended Attribute from definition
ExtendedAttribute text1TaskAttribute2 = myTextAttributeDefinition.CreateExtendedAttribute();
text1TaskAttribute2.TextValue = "Text attribute value2";
task2.ExtendedAttributes.Add(text1TaskAttribute2);
ExtendedAttribute durationExtendedAttribute2 = taskDurationAttributeDefinition.CreateExtendedAttribute();
durationExtendedAttribute2.DurationValue = asposeProject.GetDuration(5.0, TimeUnitType.ElapsedMinute);
task2.ExtendedAttributes.Add(durationExtendedAttribute2);
asposeProject.Save("CreateExtendedAttributes_out.mpp", SaveFileFormat.MPP);
}
}