Working with Extended Attributes of a Project

Microsoft Project has an extensive XML data interchange schema that makes exchanging information between applications and programming with project files easier. The schema allows you to add extended attributes to tasks, resources and assignments. This article shows how to work with Extended Attributes in Aspose.Tasks.

Working with Custom Fields using Microsoft Project

In this example, we will show how to work with Text1 extended attribute of a project for associating the information with a Task.

  1. In MSP, go to Project->Custom Fields.
  2. From here, select Task.
  3. Select Text as Custom Field Type from the Type Combo Box.
  4. Select Text1 as a custom field that you want to work with
  5. Use the “Rename” button to rename the alias of the field if it is desired and press OK button
  6. Add a new task and insert a new column to the task row with the custom field that you used in the above step

open extended attributes in Microsoft Project

Working with Custom Fields/Extended Attributes using Aspose.Tasks for .NET

Aspose.Tasks for .NET API provides the capability of creating new extended attributes as well as working with Extended attributes already present in a document. Custom fields or Extended Attributes are represented by ExtendedAttributes collection of a project in Aspose.Tasks. It contains all the extended attributes definition of a project document. Some of the mappings of MSP Custom Field definition are as shown in the image below.

switching extended attributes in Microsoft Project

Creating a New Extended Attribute and Adding it to Task

To add a new extended attribute for task or resource, we first need to define and add the extended attribute definition to the ExtendedAttributes collection. ExtendedAttributeDefinition class is used to define a new ExtendedAttribute in a project. The FieldId must be set for proper defining an Extended attribute which is linked to ExtendedAttributeTask (in case of Task) or ExtendedAttributeResource (in case of Resource). The following sample code shows how to define a new Extended Attribute for Text1 field of project. Once the Extended Attribute definition is complete, you can now create a new Extended Attribute from it and assign it to a task.

 1Project project = new Project("New Project.mpp");
 2
 3ExtendedAttributeDefinition myTextAttributeDefinition = project.ExtendedAttributes.GetById((int)ExtendedAttributeTask.Text1);
 4
 5// If the Custom field doesn't exist in Project, create it
 6if (myTextAttributeDefinition == null)
 7{
 8    myTextAttributeDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Text1, "My text field");
 9    project.ExtendedAttributes.Add(myTextAttributeDefinition);
10}
11
12// Generate Extended Attribute from definition
13ExtendedAttribute text1TaskAttribute = myTextAttributeDefinition.CreateExtendedAttribute();
14
15text1TaskAttribute.TextValue = "Text attribute value";
16
17// Add extended attribute to task
18Task task = project.RootTask.Children.Add("Task 1");
19task.ExtendedAttributes.Add(text1TaskAttribute);
20
21project.Save("CreateExtendedAttributes_out.mpp", SaveFileFormat.MPP);

Writing Updated Extended Attribute Definitions and Values to MPP

Aspose.Tasks for .NET supports updating extended attribute data in a Microsoft Project MPP file and save it back.

The example code below adds new extended attributes of the Resource and Task types to the source MPP file. The steps involved in this activity are:

  1. Create an instance of Project Reader.
  2. Read the source MPP file.
  3. Define a new extended attribute and update its values.
  4. Save the project using the Project Writer.

The following example shows setting the extended attributes of a resource.

  1Project project = new Project("New Project.mpp");
  2
  3// Add new text3 extended attribute with lookup and one lookup value
  4ExtendedAttributeDefinition taskTextAttributeDefinition = ExtendedAttributeDefinition.CreateLookupTaskDefinition(ExtendedAttributeTask.Text3, "New text3 attribute");
  5taskTextAttributeDefinition.ElementType = ElementType.Task;
  6project.ExtendedAttributes.Add(taskTextAttributeDefinition);
  7
  8Value textVal = new Value();
  9textVal.Id = 1;
 10textVal.Description = "Text value descr";
 11textVal.Val = "Text value1";
 12
 13taskTextAttributeDefinition.AddLookupValue(textVal);
 14
 15// Add new cost1 extended attribute with lookup and two cost values
 16ExtendedAttributeDefinition taskCostAttributeDefinition = ExtendedAttributeDefinition.CreateLookupTaskDefinition(ExtendedAttributeTask.Cost1, "New cost1 attribute");
 17project.ExtendedAttributes.Add(taskCostAttributeDefinition);
 18
 19Value costVal1 = new Value();
 20costVal1.Id = 2;
 21costVal1.Description = "Cost value 1 descr";
 22costVal1.Val = "99900";
 23
 24Value costVal2 = new Value();
 25costVal2.Id = 3;
 26costVal2.Description = "Cost value 2 descr";
 27costVal2.Val = "11100";
 28
 29taskCostAttributeDefinition.AddLookupValue(costVal1);
 30taskCostAttributeDefinition.AddLookupValue(costVal2);
 31
 32// Add new task and assign attribute lookup value.
 33Task task = project.RootTask.Children.Add("New task");
 34
 35ExtendedAttribute taskAttr = taskCostAttributeDefinition.CreateExtendedAttribute(costVal1);
 36task.ExtendedAttributes.Add(taskAttr);
 37
 38ExtendedAttributeDefinition taskStartAttributeDefinition = ExtendedAttributeDefinition.CreateLookupTaskDefinition(ExtendedAttributeTask.Start7, "New start 7 attribute");
 39
 40Value startVal = new Value();
 41startVal.Id = 4;
 42startVal.DateTimeValue = DateTime.Now;
 43startVal.Description = "Start 7 value description";
 44
 45taskStartAttributeDefinition.AddLookupValue(startVal);
 46
 47project.ExtendedAttributes.Add(taskStartAttributeDefinition);
 48
 49ExtendedAttributeDefinition taskFinishAttributeDefinition = ExtendedAttributeDefinition.CreateLookupTaskDefinition(ExtendedAttributeTask.Finish4, "New finish 4 attribute");
 50
 51Value finishVal = new Value();
 52finishVal.Id = 5;
 53finishVal.DateTimeValue = DateTime.Now;
 54finishVal.Description = "Finish 4 value description";
 55
 56taskFinishAttributeDefinition.ValueList.Add(finishVal);
 57
 58project.ExtendedAttributes.Add(taskFinishAttributeDefinition);
 59
 60ExtendedAttributeDefinition numberAttributeDefinition = ExtendedAttributeDefinition.CreateLookupTaskDefinition(ExtendedAttributeTask.Number20, "New number attribute");
 61
 62Value val1 = new Value();
 63val1.Id = 6;
 64val1.Val = "1";
 65val1.Description = "Number 1 value";
 66Value val2 = new Value();
 67val2.Id = 7;
 68val2.Val = "2";
 69val2.Description = "Number 2 value";
 70Value val3 = new Value();
 71val2.Id = 8;
 72val3.Val = "3";
 73val3.Description = "Number 3 value";
 74
 75numberAttributeDefinition.AddLookupValue(val1);
 76numberAttributeDefinition.AddLookupValue(val2);
 77numberAttributeDefinition.AddLookupValue(val3);
 78
 79project.ExtendedAttributes.Add(numberAttributeDefinition);
 80
 81ExtendedAttributeDefinition resourceStartAttributeDefinition = ExtendedAttributeDefinition.CreateLookupResourceDefinition(ExtendedAttributeResource.Start5, "New start5 attribute");
 82
 83Value startVal2 = new Value();
 84startVal2.Id = 9;
 85startVal2.DateTimeValue = DateTime.Now;
 86startVal2.Description = "this is start5 value descr";
 87
 88resourceStartAttributeDefinition.AddLookupValue(startVal2);
 89
 90project.ExtendedAttributes.Add(resourceStartAttributeDefinition);
 91
 92// Define a duration attribute without lookup.
 93ExtendedAttributeDefinition taskDurationAttributeDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(ExtendedAttributeTask.Duration1, "New Duration");
 94project.ExtendedAttributes.Add(taskDurationAttributeDefinition);
 95
 96// Add new task and assign duration value to the previously defined duration attribute.
 97Task timeTask = project.RootTask.Children.Add("New task");
 98
 99ExtendedAttribute durationExtendedAttribute = taskDurationAttributeDefinition.CreateExtendedAttribute();
100
101durationExtendedAttribute.DurationValue = project.GetDuration(3.0, TimeUnitType.Hour);
102timeTask.ExtendedAttributes.Add(durationExtendedAttribute);
103
104MPPSaveOptions mppSaveOptions = new MPPSaveOptions();
105mppSaveOptions.WriteViewData = true;
106
107project.Save("WriteUpdatedExtendedAttributeDefinitions_out.mpp", mppSaveOptions);
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.