How to Add Extended Attributes to Resource Assignments

Like Tasks and Resources, Extended Attributes can also be added to Resource Assignments. Aspose.Tasks for .NET API lets you extend your project data by attaching both plain and lookup Extended Attributes to resource assignments. This allows you to store additional metadata and make project reporting more flexible.

Adding Extended Attributes to Resource Assignment

In this example, we demonstrate how to create and assign an extended attribute directly to a resource assignment. This approach is useful when you need to track custom data that is not part of standard Microsoft Project fields.

 1// Create new project
 2Project project = new Project("New Project.mpp");
 3
 4// Add new task and resource
 5Task task = project.RootTask.Children.Add("Task");
 6Resource resource = project.Resources.Add("Rsc");
 7
 8// Assign the resource to the desired task
 9ResourceAssignment assignment = project.ResourceAssignments.Add(task, resource);
10
11// Custom attributes which is visible in "Resource Usage" view can be created with ExtendedAttributeDefinition.CreateResourceDefinition method.
12{
13    ExtendedAttributeDefinition resCostAttributeDefinition = ExtendedAttributeDefinition.CreateResourceDefinition(
14        CustomFieldType.Cost,
15        ExtendedAttributeResource.Cost5,
16        "My cost");
17
18    project.ExtendedAttributes.Add(resCostAttributeDefinition);
19
20    var value = resCostAttributeDefinition.CreateExtendedAttribute();
21
22    // The type of the attribute is "Cost", so we need to use "NumericValue" property.
23    value.NumericValue = 1500;
24
25    assignment.ExtendedAttributes.Add(value);
26}
27
28// Custom attributes which is visible in "Task Usage" view can be created with ExtendedAttributeDefinition.CreateTaskDefinition method
29{
30    ExtendedAttributeDefinition taskCostAttributeDefinition = ExtendedAttributeDefinition.CreateTaskDefinition(
31        CustomFieldType.Cost,
32        ExtendedAttributeTask.Cost5,
33        "My cost for task");
34
35    project.ExtendedAttributes.Add(taskCostAttributeDefinition);
36
37    var value = taskCostAttributeDefinition.CreateExtendedAttribute();
38
39    // The type of the attribute is "Cost", so we need to use "NumericValue" property.
40    value.NumericValue = 2300;
41
42    assignment.ExtendedAttributes.Add(value);
43}
44
45project.Save("AddExtendedAttributesToResourceAssignment_out.mpp", SaveFileFormat.MPP);

Adding Lookup Extended Attributes to Resource Assignment

The following code shows how to use lookup values for extended attributes. With lookup attributes, you can ensure that resource assignment data conforms to predefined lists, reducing human error and keeping project information consistent.

 1// Create new project
 2Project project = new Project("New Project.mpp");
 3
 4// Assign resource "1 TRG: Trade Group" to the "TASK 1" by creating a ResourceAssignment object.
 5Resource resource = project.Resources.GetById(1);
 6Task task = project.RootTask.Children.GetById(1);
 7
 8ResourceAssignment assignment = project.ResourceAssignments.Add(task, resource);
 9
10// Create custom attribute definition with lookup.
11ExtendedAttributeDefinition resCostAttr = ExtendedAttributeDefinition.CreateLookupResourceDefinition(
12    CustomFieldType.Cost,
13    ExtendedAttributeResource.Cost5,
14    "My lookup resource cost");
15project.ExtendedAttributes.Add(resCostAttr);
16
17var value1 = new Value { NumberValue = 1500, Description = "Val 1", Id = 1, Val = "1500" };
18
19resCostAttr.AddLookupValue(value1);
20
21resCostAttr.AddLookupValue(new Value { NumberValue = 2500, Description = "Val 2", Id = 2 });
22
23// This value can be seen in "Resource usage" view of MS Project.
24var attributeValue = resCostAttr.CreateExtendedAttribute(value1);
25assignment.ExtendedAttributes.Add(attributeValue);
26
27// Create custom attribute definition with lookup.
28ExtendedAttributeDefinition taskCostAttr = ExtendedAttributeDefinition.CreateLookupTaskDefinition(
29    ExtendedAttributeTask.Cost4,
30    "My lookup task cost");
31
32project.ExtendedAttributes.Add(taskCostAttr);
33
34var taskLookupValue1 = new Value { NumberValue = 18, Description = "Task val 1", Id = 3, Val = "18" };
35taskCostAttr.AddLookupValue(taskLookupValue1);
36
37resCostAttr.AddLookupValue(new Value { NumberValue = 30, Description = "Task val 2", Id = 4 });
38
39// This value can be seen in "Task usage" view of MS Project.
40assignment.ExtendedAttributes.Add(taskCostAttr.CreateExtendedAttribute(taskLookupValue1));
41
42project.Save("AddExtendedAttributesToRAWithLookUp_out.mpp", SaveFileFormat.MPP);

FAQ

Q: Do I need Microsoft Project installed to work with Extended Attributes?

Q: Can I define both plain and lookup extended attributes in the same project?

Q: Will extended attributes added through Aspose.Tasks appear when opening the project in Microsoft Project?

Conclusion

Adding extended attributes to resource assignments in Aspose.Tasks for .NET provides flexibility for managing additional project data. Whether you use plain or lookup attributes, you can enrich your project files with custom fields, enforce data integrity, and make your reporting more powerful without requiring Microsoft Project to be installed. This functionality makes Aspose.Tasks a reliable choice for professional project management automation in C#.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.